Method: AttributesMap#deep_merge

Defined in:
lib/mofa/attributes_map.rb

#deep_merge(attr_hash, attr_hash_local) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mofa/attributes_map.rb', line 59

def deep_merge(attr_hash, attr_hash_local)
  new_attr_hash = Marshal.load(Marshal.dump(attr_hash))
  attr_hash.each do |key, value|
    if attr_hash_local.key?(key)
      if value.is_a?(Hash) && attr_hash_local[key].is_a?(Hash)
        new_attr_hash[key] = deep_merge(value, attr_hash_local[key])
      else
        new_attr_hash[key] = attr_hash_local[key]
      end
    end
  end
  # and now add all attributes that are in attr_hash_local but not in attr_hash
  attr_hash_local.each do |key, value|
    unless attr_hash.key?(key)
      new_attr_hash.store(key, value)
    end
  end

  new_attr_hash
end