Class: Malevich::Hashie

Inherits:
Hash
  • Object
show all
Defined in:
lib/malevich/support/hash.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object



21
22
23
# File 'lib/malevich/support/hash.rb', line 21

def method_missing(method_name, *args, &blk)
  self.[](method_name.to_sym, &blk)
end

Instance Method Details

#deep_merge(other_hash, &block) ⇒ Object



5
6
7
# File 'lib/malevich/support/hash.rb', line 5

def deep_merge(other_hash, &block)
  dup.deep_merge!(other_hash, &block)
end

#deep_merge!(other_hash, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/malevich/support/hash.rb', line 9

def deep_merge!(other_hash, &block)
  other_hash.each_pair do |k,v|
    tv = self[k]
    if tv.is_a?(Hash) && v.is_a?(Hash)
      self[k] = tv.deep_merge(v, &block)
    else
      self[k] = block && tv ? block.call(k, tv, v) : v
    end
  end
  self
end