Class: Guacamole::DocumentModelMapper
- Inherits:
-
Object
- Object
- Guacamole::DocumentModelMapper
- Defined in:
- lib/guacamole/document_model_mapper.rb
Overview
If you plan to bring your own DocumentModelMapper
please consider using an IdentityMap.
This is the default mapper class to map between Ashikawa::Core::Document and Guacamole::Model instances.
If you want to build your own mapper, you have to build at least the
document_to_model
and model_to_document
methods.
Defined Under Namespace
Classes: Attribute
Instance Attribute Summary collapse
-
#attributes ⇒ Array<Attribute>
readonly
The list of Attributes to treat specially during the mapping process.
-
#model_class ⇒ class
readonly
The class to map to.
-
#models_to_embed ⇒ Array
readonly
The arrays embedded in this model.
Class Method Summary collapse
-
.collection_for(model_name) ⇒ class
construct the collection class for a given model name.
Instance Method Summary collapse
-
#attribute(attribute_name, options = {}) ⇒ Object
Mark an attribute of the model to be specially treated during mapping.
-
#collection_for(model_name = model_class.name) ⇒ class
construct the collection class for a given model name.
-
#document_to_model(document) ⇒ Model
Map a document to a model.
-
#edge_attributes ⇒ Array<Attribute>
Returns a list of attributes that have an Edge class configured.
-
#embeds(model_name) ⇒ Object
Declare a model to be embedded.
-
#initialize(model_class, identity_map = IdentityMap) ⇒ DocumentModelMapper
constructor
Create a new instance of the mapper.
-
#model_to_document(model) ⇒ Ashikawa::Core::Document
Map a model to a document.
-
#responsible_for?(model) ⇒ Boolean
Is this Mapper instance responsible for mapping the given model.
Constructor Details
#initialize(model_class, identity_map = IdentityMap) ⇒ DocumentModelMapper
Create a new instance of the mapper
You have to provide the model class you want to map to. The Document class is always Ashikawa::Core::Document
109 110 111 112 113 114 |
# File 'lib/guacamole/document_model_mapper.rb', line 109 def initialize(model_class, identity_map = IdentityMap) @model_class = model_class @identity_map = identity_map @models_to_embed = [] @attributes = [] end |
Instance Attribute Details
#attributes ⇒ Array<Attribute> (readonly)
The list of Attributes to treat specially during the mapping process
101 102 103 |
# File 'lib/guacamole/document_model_mapper.rb', line 101 def attributes @attributes end |
#model_class ⇒ class (readonly)
The class to map to
91 92 93 |
# File 'lib/guacamole/document_model_mapper.rb', line 91 def model_class @model_class end |
#models_to_embed ⇒ Array (readonly)
The arrays embedded in this model
96 97 98 |
# File 'lib/guacamole/document_model_mapper.rb', line 96 def @models_to_embed end |
Class Method Details
.collection_for(model_name) ⇒ class
This is an class level alias for #collection_for
construct the collection class for a given model name.
126 127 128 |
# File 'lib/guacamole/document_model_mapper.rb', line 126 def collection_for(model_name) "#{model_name.to_s.classify.pluralize}Collection".constantize end |
Instance Method Details
#attribute(attribute_name, options = {}) ⇒ Object
Mark an attribute of the model to be specially treated during mapping
233 234 235 |
# File 'lib/guacamole/document_model_mapper.rb', line 233 def attribute(attribute_name, = {}) @attributes << Attribute.new(attribute_name, ) end |
#collection_for(model_name = model_class.name) ⇒ class
As of now this is some kind of placeholder method. As soon as we implement the configuration of the mapping (#12) this will change. Still the Guacamole::DocumentModelMapper seems to be a good place for this functionality.
construct the collection class for a given model name.
142 143 144 |
# File 'lib/guacamole/document_model_mapper.rb', line 142 def collection_for(model_name = model_class.name) self.class.collection_for model_name end |
#document_to_model(document) ⇒ Model
Map a document to a model
Sets the revision, key and all attributes on the model
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/guacamole/document_model_mapper.rb', line 152 def document_to_model(document) identity_map.retrieve_or_store model_class, document.key do model = model_class.new(document.to_h) model.key = document.key model.rev = document.revision (model) model end end |
#edge_attributes ⇒ Array<Attribute>
Returns a list of attributes that have an Edge class configured
240 241 242 |
# File 'lib/guacamole/document_model_mapper.rb', line 240 def edge_attributes attributes.select(&:map_via_edge?) end |
#embeds(model_name) ⇒ Object
Declare a model to be embedded
With embeds you can specify that the document in the collection embeds a document that should be mapped to a certain model. Your model has to specify an attribute with the type Array (of this model).
209 210 211 |
# File 'lib/guacamole/document_model_mapper.rb', line 209 def (model_name) @models_to_embed << model_name end |
#model_to_document(model) ⇒ Ashikawa::Core::Document
Map a model to a document
This will include all embedded models
171 172 173 174 175 176 177 178 |
# File 'lib/guacamole/document_model_mapper.rb', line 171 def model_to_document(model) document = model.attributes.dup.except(:key, :rev) (model, document) (document) document end |
#responsible_for?(model) ⇒ Boolean
Is this Mapper instance responsible for mapping the given model
248 249 250 |
# File 'lib/guacamole/document_model_mapper.rb', line 248 def responsible_for?(model) model.instance_of?(model_class) end |