Class: Lotus::Model::Mapper
- Inherits:
-
Object
- Object
- Lotus::Model::Mapper
- Defined in:
- lib/lotus/model/mapper.rb
Overview
A persistence mapper that keeps entities independent from database details.
This is database independent. It can work with SQL, document, and even with key/value stores.
Instance Attribute Summary collapse
- #collections ⇒ Object readonly private
Instance Method Summary collapse
-
#collection(name, &blk) ⇒ Object
Maps a collection.
-
#initialize(coercer = nil, &blk) ⇒ Lotus::Model::Mapper
constructor
Instantiate a mapper.
-
#load!(adapter = nil) ⇒ Object
Loads the internals of the mapper, in order to guarantee thread safety.
Constructor Details
#initialize(coercer = nil, &blk) ⇒ Lotus::Model::Mapper
Instantiate a mapper.
It accepts an optional argument (‘coercer`) a class that defines the policies for entities translations from/to the database.
If provided, this class must implement the following interface:
* #initialize(collection) # Lotus::Model::Mapping::Collection
* #to_record(entity) # translates an entity to the database type
* #from_record(record) # translates a record into an entity
* #deserialize_*(value) # a set of methods, one for each database column.
If not given, it uses ‘Lotus::Model::Mapping::Coercer`, by default.
81 82 83 84 85 86 |
# File 'lib/lotus/model/mapper.rb', line 81 def initialize(coercer = nil, &blk) @coercer = coercer || Mapping::Coercer @collections = {} instance_eval(&blk) if block_given? end |
Instance Attribute Details
#collections ⇒ 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/mapper.rb', line 54 def collections @collections end |
Instance Method Details
#collection(name, &blk) ⇒ Object
Maps a collection.
A collection is a set of homogeneous records. Think of a table of a SQL database or about collection of MongoDB.
101 102 103 104 105 106 107 |
# File 'lib/lotus/model/mapper.rb', line 101 def collection(name, &blk) if block_given? @collections[name] = Mapping::Collection.new(name, @coercer, &blk) else @collections[name] or raise Mapping::UnmappedCollectionError.new(name) end end |
#load!(adapter = nil) ⇒ Object
Loads the internals of the mapper, in order to guarantee thread safety.
This method MUST be invoked as the last thing before of start using the application.
115 116 117 118 119 120 121 |
# File 'lib/lotus/model/mapper.rb', line 115 def load!(adapter = nil) @collections.each_value do |collection| collection.adapter = adapter collection.load! end self end |