abstract class Jennifer::Model::Resource

Overview

Base abstract class for a database entity.

Included Modules

Extended Modules

Direct Known Subclasses

Defined in:

jennifer/model/resource.cr

Class Method Summary

Instance Method Summary

Instance methods inherited from module Jennifer::Model::RelationDefinition

append_relation(name : String, hash) append_relation, get_relation(name : String) get_relation, relation_retrieved(name : String) relation_retrieved, set_inverse_of(name : String, object) set_inverse_of

Instance methods inherited from module Jennifer::Model::Translation

class_name : String class_name, human_attribute_name(attribute : String | Symbol) human_attribute_name, lookup_ancestors(&) lookup_ancestors

Class Method Detail

def self.adapter #

Returns adapter instance.


[View source]
def self.all #

Implementation of AbstractClassMethods.all.

User.all.where { _name == "John" }

[View source]
def self.build(values : Hash(Symbol, Jennifer::DBAny) | NamedTuple) #

Alias for .new.


[View source]
def self.build(values : Hash(String, Jennifer::DBAny)) #

Alias for .new.


[View source]
def self.build(**values) #

Alias for .new.


[View source]
def self.c(name : String | Symbol, relation) #

[View source]
def self.c(name : String | Symbol) #

Returns criterion for column name of resource's table.

User.c(:email) # => users.email

[View source]
def self.context #

Returns QueryBuilder::ExpressionBuilder object of this resource's table.

User.context.sql("ABS(1.2)")

[View source]
def self.read_adapter #

Returns adapter used to read resource from the database.


[View source]
def self.relation(name) #

[View source]
def self.star #

Returns star field statement for current resource's table.

User.star # => users.*

[View source]
def self.table_name(value : String | Symbol) #

Sets custom table name.

Specified table name should include table name prefix as it is used "as is".


[View source]
def self.table_name : String #

Returns resource's table name.

User.table_name        # "users"
Admin::User.table_name # "admin_users"

class Admin::Post < Jennifer::Model::Base
  # ...

  def self.table_prefix; end
end

Admin::Post.table_name # "posts"

[View source]
def self.transaction(&) #

Starts database transaction.

For more details see Jennifer::Adapter::Transactions.

User.transaction do
  Post.create
end

[View source]
def self.where(&) #

Is a shortcut for .all.where call.

User.where { _name == "John" }

[View source]
def self.where(conditions : Hash(Symbol, _)) #

Is a shortcut for .all.where call.

User.where { _name == "John" }

[View source]
def self.write_adapter #

Returns adapter used to write resource to the database.


[View source]

Instance Method Detail

def append_relation(name : String, hash) #

[View source]
abstract def attribute(name : String | Symbol, raise_exception : Bool = true) #

Returns value of attribute name.

It method doesn't invoke getter. If no attribute with given name is found - BaseException is raised. To prevent this and return nil instead - pass false for raise_exception.

contact.attribute(:name)          # => Jennifer::DBAny
contact.attribute("age")          # => Jennifer::DBAny
contact.attribute(:salary)        # => Jennifer::BaseException is raised
contact.attribute(:salary, false) # => nil

[View source]
def get_relation(name : String) #

[View source]
def inspect(io) : Nil #

Returns a string containing a human-readable representation of object.

Address.new.inspect
# => "#<Address:0x7f532bdd5340 id: nil, street: "Ant st. 69", contact_id: nil, created_at: nil, updated_at: nil>"

[View source]
abstract def primary #

Returns value of primary field

Is generated by .mapping macro.


[View source]
def set_inverse_of(name : String, object) #

[View source]
abstract def to_h #

Returns hash with model attributes; keys are symbols.

Is generated by .mapping macro.

contact.to_h # => { name: "Jennifer", age: 2 }

[View source]
def to_json(only : Array(String)? = nil, except : Array(String)? = nil) #

Returns a JSON string representing the resource.

Without any argument or block passed in all resource columns are serialized.

user.to_json
# => {"id": 1,"name": "John Smith", "age": 42,"admin":false}

The only argument allows to specify the exact collection of fields to be serialized:

user.to_json(only: %w[id name])
# => {"id": 1,"name": "John Smith"}

The except argument allows to specify which field should not be serialized:

user.to_json(except: %w[id name])
# => {"age": 42,"admin":false}

Only one argument only or except can be specified at a time.

Also the block can be specified to serialize extra fields. As arguments block receives json builder and resource itself

user.to_json do |json|
  json.field "first_name", user.name.split(" ")[0]
end
# => {"id": 1,"name": "John Smith", "age": 42,"admin":false, "first_name": "John"}

[View source]
def to_json(only : Array(String)? = nil, except : Array(String)? = nil, &) #

[View source]
def to_json(json : JSON::Builder) #

[View source]
def to_json(json : JSON::Builder, only : Array(String)? = nil, except : Array(String)? = nil, &) #

[View source]
abstract def to_str_h #

Returns hash with model attributes; keys are strings.

Is generated by .mapping macro.

contact.to_h # => { "name" => "Jennifer", "age" => 2 }

[View source]