class Locomotive::Steam::Models::Mapper
Constants
- ASSOCIATION_CLASSES
Attributes
associations[R]
default_attributes[R]
localized_attributes[R]
name[R]
options[R]
Public Class Methods
new(name, options, repository, &block)
click to toggle source
# File lib/locomotive/steam/models/mapper.rb, line 15 def initialize(name, options, repository, &block) @name, @options, @repository = name, options, repository @localized_attributes = [] @default_attributes = [] @associations = [] @entity_map = {} instance_eval(&block) if block_given? end
Public Instance Methods
association(type, name, repository_klass, options = nil, &block)
click to toggle source
# File lib/locomotive/steam/models/mapper.rb, line 47 def association(type, name, repository_klass, options = nil, &block) @associations << [type, name.to_sym, repository_klass, options || {}, block] end
default_attribute(name, value)
click to toggle source
# File lib/locomotive/steam/models/mapper.rb, line 37 def default_attribute(name, value) @default_attributes += [[name.to_sym, value]] end
deserialize(attributes)
click to toggle source
# File lib/locomotive/steam/models/mapper.rb, line 66 def deserialize(attributes) build_localized_attributes(attributes) build_associations(attributes) attributes end
entity_klass()
click to toggle source
# File lib/locomotive/steam/models/mapper.rb, line 89 def entity_klass options[:entity] end
i18n_value_of(entity, name, locale)
click to toggle source
# 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()
click to toggle source
# File lib/locomotive/steam/models/mapper.rb, line 98 def reset_entity_map @entity_map = {} end
serialize(entity)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
attach_entity_to_associations(entity)
click to toggle source
# File lib/locomotive/steam/models/mapper.rb, line 127 def attach_entity_to_associations(entity) @associations.each do |(type, name, _)| association = entity[name] association.__attach__(entity) entity.associations[name] = association end end
build_associations(attributes)
click to toggle source
create a proxy class for each association
# File lib/locomotive/steam/models/mapper.rb, line 113 def build_associations(attributes) @associations.each do |(type, name, repository_klass, options, block)| klass = ASSOCIATION_CLASSES[type] _options = options.merge(association_name: name, mapper_name: self.name) attributes[name] = (if type == :embedded klass.new(repository_klass, attributes[name], @repository.scope, _options) else klass.new(repository_klass, @repository.scope, @repository.adapter, _options, &block) end) end end
build_localized_attributes(attributes)
click to toggle source
create a proxy class for each localized attribute
# File lib/locomotive/steam/models/mapper.rb, line 105 def build_localized_attributes(attributes) @localized_attributes.each do |name| _name = name.to_sym attributes[_name] = I18nField.new(_name, attributes[name.to_s] || attributes[_name]) end end
cache_entity(entity_klass, attributes) { || ... }
click to toggle source
# File lib/locomotive/steam/models/mapper.rb, line 143 def cache_entity(entity_klass, attributes, &block) entity_id = attributes['_id'] || attributes[:_id] # FIXME: in Wagon, we deal with symbols return yield if entity_id.blank? key = "#{entity_klass.to_s}-#{entity_id}" if (entity = @entity_map[key]).nil? entity = @entity_map[key] = yield end entity end
set_default_attributes(entity)
click to toggle source
# File lib/locomotive/steam/models/mapper.rb, line 136 def set_default_attributes(entity) @default_attributes.each do |(name, value)| _value = value.respond_to?(:call) ? value.call(@repository) : value entity.send(:"#{name}=", _value) end end