Class: AqumulateAPI::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/aqumulate_api/entity.rb

Constant Summary collapse

ATTR_MAP =
{}
SOURCE_ASSOCIATIONS =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Entity

Returns a new instance of Entity.



7
8
9
# File 'lib/aqumulate_api/entity.rb', line 7

def initialize(attributes = {})
  attributes.each { |k, v| instance_variable_set("@#{k}", v) }
end

Class Method Details

.from_source(source) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/aqumulate_api/entity.rb', line 11

def self.from_source(source)
  entity = new({})

  self::ATTR_MAP.invert.each do |k, v|
    entity.send("#{v.to_s}=", source[k])
  end

  self::SOURCE_ASSOCIATIONS.each do |association|
    next if source[association[:key]].nil? || source[association[:key]].empty?
    association_set = entity.send(association[:attr])

    source[association[:key]].each do |association_source|
      association_set << association[:class].from_source(association_source)
    end

    entity.send("#{association[:attr].to_s}=", association_set)
  end

  entity
end