Class: Locomotive::Steam::Models::Mapper
- Inherits:
-
Object
- Object
- Locomotive::Steam::Models::Mapper
- Defined in:
- lib/locomotive/steam/models/mapper.rb
Constant Summary collapse
- ASSOCIATION_CLASSES =
{ embedded: EmbeddedAssociation, belongs_to: BelongsToAssociation, has_many: HasManyAssociation, many_to_many: ManyToManyAssociation }.freeze
Instance Attribute Summary collapse
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#default_attributes ⇒ Object
readonly
Returns the value of attribute default_attributes.
-
#localized_attributes(*args) ⇒ Object
readonly
Returns the value of attribute localized_attributes.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #association(type, name, repository_klass, options = nil, &block) ⇒ Object
- #default_attribute(name, value) ⇒ Object
- #deserialize(attributes) ⇒ Object
- #entity_klass ⇒ Object
- #i18n_value_of(entity, name, locale) ⇒ Object
-
#initialize(name, options, repository, &block) ⇒ Mapper
constructor
A new instance of Mapper.
- #reset_entity_map ⇒ Object
- #serialize(entity) ⇒ Object
- #to_entity(attributes) ⇒ Object
Constructor Details
#initialize(name, options, repository, &block) ⇒ Mapper
Returns a new instance of Mapper.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/locomotive/steam/models/mapper.rb', line 15 def initialize(name, , repository, &block) @name, @options, @repository = name, , repository @localized_attributes = [] @default_attributes = [] @associations = [] @entity_map = {} instance_eval(&block) if block_given? end |
Instance Attribute Details
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
13 14 15 |
# File 'lib/locomotive/steam/models/mapper.rb', line 13 def associations @associations end |
#default_attributes ⇒ Object (readonly)
Returns the value of attribute default_attributes.
13 14 15 |
# File 'lib/locomotive/steam/models/mapper.rb', line 13 def default_attributes @default_attributes end |
#localized_attributes(*args) ⇒ Object (readonly)
Returns the value of attribute localized_attributes.
13 14 15 |
# File 'lib/locomotive/steam/models/mapper.rb', line 13 def localized_attributes @localized_attributes end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/locomotive/steam/models/mapper.rb', line 13 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/locomotive/steam/models/mapper.rb', line 13 def @options end |
Instance Method Details
#association(type, name, repository_klass, options = nil, &block) ⇒ Object
47 48 49 |
# File 'lib/locomotive/steam/models/mapper.rb', line 47 def association(type, name, repository_klass, = nil, &block) @associations << [type, name.to_sym, repository_klass, || {}, block] end |
#default_attribute(name, value) ⇒ Object
37 38 39 |
# File 'lib/locomotive/steam/models/mapper.rb', line 37 def default_attribute(name, value) @default_attributes += [[name.to_sym, value]] end |
#deserialize(attributes) ⇒ Object
66 67 68 69 70 |
# File 'lib/locomotive/steam/models/mapper.rb', line 66 def deserialize(attributes) build_localized_attributes(attributes) build_associations(attributes) attributes end |
#entity_klass ⇒ Object
89 90 91 |
# File 'lib/locomotive/steam/models/mapper.rb', line 89 def entity_klass [:entity] end |
#i18n_value_of(entity, name, locale) ⇒ Object
93 94 95 96 |
# File 'lib/locomotive/steam/models/mapper.rb', line 93 def i18n_value_of(entity, name, locale) value = entity.send(name.to_sym) value.respond_to?(:translations) ? value[locale] : value end |
#reset_entity_map ⇒ Object
98 99 100 |
# File 'lib/locomotive/steam/models/mapper.rb', line 98 def reset_entity_map @entity_map = {} end |
#serialize(entity) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/locomotive/steam/models/mapper.rb', line 72 def serialize(entity) entity.serialize.tap do |attributes| # scope @repository.scope.apply(attributes) # localized fields @localized_attributes.each do |name| entity.send(name).serialize(attributes) end # association name -> id (belongs_to) or ids (many_to_many) (entity.associations || {}).each do |name, association| association.__serialize__(attributes) end end end |
#to_entity(attributes) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/locomotive/steam/models/mapper.rb', line 51 def to_entity(attributes) cache_entity(entity_klass, attributes) do entity_klass.new(deserialize(attributes)).tap do |entity| set_default_attributes(entity) entity.localized_attributes = @localized_attributes_hash || {} entity.associations = {} attach_entity_to_associations(entity) entity.base_url = @repository.base_url(entity) end end end |