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, klass, 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
92 93 94 95 96 97 |
# File 'lib/lotus/model/mapping/collection.rb', line 92 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.
79 80 81 |
# File 'lib/lotus/model/mapping/collection.rb', line 79 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.
73 74 75 |
# File 'lib/lotus/model/mapping/collection.rb', line 73 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.
67 68 69 |
# File 'lib/lotus/model/mapping/collection.rb', line 67 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.
61 62 63 |
# File 'lib/lotus/model/mapping/collection.rb', line 61 def name @name end |
Instance Method Details
#attribute(name, klass, 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:
* Array
* Boolean
* Date
* DateTime
* Float
* Hash
* Integer
* Set
* String
* Symbol
* Time
351 352 353 |
# File 'lib/lotus/model/mapping/collection.rb', line 351 def attribute(name, klass, = {}) @attributes[name] = [klass, (.fetch(:as) { name }).to_sym] 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.
371 372 373 374 375 |
# File 'lib/lotus/model/mapping/collection.rb', line 371 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.
384 385 386 |
# File 'lib/lotus/model/mapping/collection.rb', line 384 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 = {})`.
132 133 134 135 136 137 138 |
# File 'lib/lotus/model/mapping/collection.rb', line 132 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.
229 230 231 232 233 234 235 |
# File 'lib/lotus/model/mapping/collection.rb', line 229 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.
392 393 394 395 396 397 398 |
# File 'lib/lotus/model/mapping/collection.rb', line 392 def load! _load_entity! _load_repository! _load_coercer! _configure_repository! end |
#repository(klass = nil) ⇒ Object
Defines the repository that interacts with this collection.
173 174 175 176 177 178 179 |
# File 'lib/lotus/model/mapping/collection.rb', line 173 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.
361 362 363 |
# File 'lib/lotus/model/mapping/collection.rb', line 361 def serialize(entity) @coercer.to_record(entity) end |