Class: Datory::Attributes::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/datory/attributes/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, direction, attributes, collection_of_attributes) ⇒ Model

Returns a new instance of Model.



10
11
12
13
14
15
# File 'lib/datory/attributes/model.rb', line 10

def initialize(context, direction, attributes, collection_of_attributes)
  @context = context
  @direction = direction
  @attributes = attributes
  @collection_of_attributes = collection_of_attributes
end

Class Method Details

.build!Object



6
7
8
# File 'lib/datory/attributes/model.rb', line 6

def self.build!(...)
  new(...).build!
end

Instance Method Details

#build!Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/datory/attributes/model.rb', line 17

def build!
  @collection_of_attributes.each do |attribute|
    attribute_name = @direction.serialization? ? attribute.to.name : attribute.from.name
    attribute_value = @attributes.fetch(attribute_name, nil)

    assign_attribute_for(@context, name: attribute_name, value: attribute_value)
  end

  @context
end