Class: Lotus::Model::Mapping::Collection
- Inherits:
-
Object
- Object
- Lotus::Model::Mapping::Collection
- Defined in:
- lib/lotus/model/mapping/collection.rb
Overview
Maps a collection and its attributes.
A collection is a set of homogeneous records. Think of a table of a SQL database or about collection of MongoDB.
This is database independent. It can work with SQL, document, and even with key/value stores.
Constant Summary collapse
- REPOSITORY_SUFFIX =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Repository name suffix
'Repository'.freeze
Instance Attribute Summary collapse
- #adapter ⇒ Object private
- #attributes ⇒ Object readonly private
- #coercer_class ⇒ Object readonly private
- #name ⇒ Object readonly private
Instance Method Summary collapse
-
#attribute(name, coercer, options = {}) ⇒ Object
Map an attribute.
-
#deserialize(records) ⇒ Object
private
Deserialize a set of records fetched from the database.
-
#deserialize_attribute(attribute, value) ⇒ Object
private
Deserialize only one attribute from a raw value.
-
#entity(klass = nil) ⇒ Object
Defines the entity that is persisted with this collection.
-
#identity(name = nil) ⇒ Object
Defines the identity for a collection.
-
#initialize(name, coercer_class, &blk) ⇒ Collection
constructor
Instantiate a new collection.
-
#load! ⇒ Object
private
Loads the internals of the mapper, in order to guarantee thread safety.
-
#repository(klass = nil) ⇒ Object
Defines the repository that interacts with this collection.
-
#serialize(entity) ⇒ Object
private
Serializes an entity to be persisted in the database.
Constructor Details
#initialize(name, coercer_class, &blk) ⇒ Collection
Instantiate a new collection
74 75 76 77 78 79 |
# File 'lib/lotus/model/mapping/collection.rb', line 74 def initialize(name, coercer_class, &blk) @name = name @coercer_class = coercer_class @attributes = {} instance_eval(&blk) if block_given? end |
Instance Attribute Details
#adapter ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
61 62 63 |
# File 'lib/lotus/model/mapping/collection.rb', line 61 def adapter @adapter end |
#attributes ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
55 56 57 |
# File 'lib/lotus/model/mapping/collection.rb', line 55 def attributes @attributes end |
#coercer_class ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
49 50 51 |
# File 'lib/lotus/model/mapping/collection.rb', line 49 def coercer_class @coercer_class end |
#name ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
43 44 45 |
# File 'lib/lotus/model/mapping/collection.rb', line 43 def name @name end |
Instance Method Details
#attribute(name, coercer, options = {}) ⇒ Object
Map an attribute.
An attribute defines a property of an object. This is storage independent. For instance, it can map an SQL column, a MongoDB attribute or everything that makes sense for your database.
Each attribute defines a Ruby type, to coerce that value from the database. This fixes a huge problem, because database types don’t match Ruby types. Think of Redis, where everything is stored as a string or integer, the mapper translates values from/to the database.
It supports the following types (coercers):
* Array
* Boolean
* Date
* DateTime
* Float
* Hash
* Integer
* BigDecimal
* Set
* String
* Symbol
* Time
387 388 389 |
# File 'lib/lotus/model/mapping/collection.rb', line 387 def attribute(name, coercer, = {}) @attributes[name] = Attribute.new(name, coercer, ) end |
#deserialize(records) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deserialize a set of records fetched from the database.
407 408 409 410 411 |
# File 'lib/lotus/model/mapping/collection.rb', line 407 def deserialize(records) records.map do |record| @coercer.from_record(record) end end |
#deserialize_attribute(attribute, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deserialize only one attribute from a raw value.
420 421 422 |
# File 'lib/lotus/model/mapping/collection.rb', line 420 def deserialize_attribute(attribute, value) @coercer.public_send(:"deserialize_#{ attribute }", value) end |
#entity(klass = nil) ⇒ Object
Defines the entity that is persisted with this collection.
The entity can be any kind of object as long as it implements the following interface: ‘#initialize(attributes = {})`.
114 115 116 117 118 119 120 |
# File 'lib/lotus/model/mapping/collection.rb', line 114 def entity(klass = nil) if klass @entity = klass else @entity end end |
#identity(name = nil) ⇒ Object
Defines the identity for a collection.
An identity is a unique value that identifies a record. If used with an SQL table it corresponds to the primary key.
This is an optional feature. By default the system assumes that your identity is ‘:id`. If this is the case, you can omit the value, otherwise you have to specify it.
211 212 213 214 215 216 217 |
# File 'lib/lotus/model/mapping/collection.rb', line 211 def identity(name = nil) if name @identity = name else @identity || :id end end |
#load! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Loads the internals of the mapper, in order to guarantee thread safety.
428 429 430 431 432 433 434 |
# File 'lib/lotus/model/mapping/collection.rb', line 428 def load! _load_entity! _load_repository! _load_coercer! _configure_repository! end |
#repository(klass = nil) ⇒ Object
Defines the repository that interacts with this collection.
155 156 157 158 159 160 161 |
# File 'lib/lotus/model/mapping/collection.rb', line 155 def repository(klass = nil) if klass @repository = klass else @repository ||= default_repository_klass end end |
#serialize(entity) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Serializes an entity to be persisted in the database.
397 398 399 |
# File 'lib/lotus/model/mapping/collection.rb', line 397 def serialize(entity) @coercer.to_record(entity) end |