Class: Locomotive::Steam::Models::Mapper

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options, repository, &block)
  @name, @options, @repository = name, options, repository

  @localized_attributes = []
  @default_attributes   = []
  @associations         = []

  @entity_map           = {}

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



13
14
15
# File 'lib/locomotive/steam/models/mapper.rb', line 13

def associations
  @associations
end

#default_attributesObject (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

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/locomotive/steam/models/mapper.rb', line 13

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/locomotive/steam/models/mapper.rb', line 13

def options
  @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, options = nil, &block)
  @associations << [type, name.to_sym, repository_klass, options || {}, 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_klassObject



89
90
91
# File 'lib/locomotive/steam/models/mapper.rb', line 89

def entity_klass
  options[: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_mapObject



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