Module: Kalimba::AttributeAssignment

Included in:
Resource
Defined in:
lib/kalimba/attribute_assignment.rb

Instance Method Summary collapse

Instance Method Details

#assign_attributes(new_attributes = {}, options = {}) ⇒ void

This method returns an undefined value.

Assign attributes from the given hash

Parameters:

  • new_attributes (Hash<[Symbol, String] => Any>) (defaults to: {})
  • options (Hash) (defaults to: {})


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kalimba/attribute_assignment.rb', line 8

def assign_attributes(new_attributes = {}, options = {})
  return if new_attributes.blank?

  attributes = new_attributes.stringify_keys
  multi_parameter_attributes = []
  nested_parameter_attributes = []

  attributes.each do |k, v|
    if k.include?("(")
      multi_parameter_attributes << [ k, v ]
    elsif respond_to?("#{k}=")
      if v.is_a?(Hash)
        nested_parameter_attributes << [ k, v ]
      else
        send("#{k}=", v)
      end
    else
      raise UnknownAttributeError, "unknown attribute: #{k}"
    end
  end

  # assign any deferred nested attributes after the base attributes have been set
  nested_parameter_attributes.each do |k,v|
    send("#{k}=", v)
  end

  assign_multiparameter_attributes(multi_parameter_attributes)
end