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
73 74 75 76 77 78 |
# File 'lib/lotus/model/mapping/collection.rb', line 73 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.
60 61 62 |
# File 'lib/lotus/model/mapping/collection.rb', line 60 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.
54 55 56 |
# File 'lib/lotus/model/mapping/collection.rb', line 54 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.
48 49 50 |
# File 'lib/lotus/model/mapping/collection.rb', line 48 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.
42 43 44 |
# File 'lib/lotus/model/mapping/collection.rb', line 42 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
* BigDecimal
* Set
* String
* Symbol
* Time
333 334 335 |
# File 'lib/lotus/model/mapping/collection.rb', line 333 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.
353 354 355 356 357 |
# File 'lib/lotus/model/mapping/collection.rb', line 353 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.
366 367 368 |
# File 'lib/lotus/model/mapping/collection.rb', line 366 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 = {})`.
113 114 115 116 117 118 119 |
# File 'lib/lotus/model/mapping/collection.rb', line 113 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.
210 211 212 213 214 215 216 |
# File 'lib/lotus/model/mapping/collection.rb', line 210 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.
374 375 376 377 378 379 380 |
# File 'lib/lotus/model/mapping/collection.rb', line 374 def load! _load_entity! _load_repository! _load_coercer! _configure_repository! end |
#repository(klass = nil) ⇒ Object
Defines the repository that interacts with this collection.
154 155 156 157 158 159 160 |
# File 'lib/lotus/model/mapping/collection.rb', line 154 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.
343 344 345 |
# File 'lib/lotus/model/mapping/collection.rb', line 343 def serialize(entity) @coercer.to_record(entity) end |