Class: Jahuty::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/jahuty/util.rb

Overview

Utility methods.

Class Method Summary collapse

Class Method Details

.deep_merge(first_hash, other_hash, &block) ⇒ Object

Deeply merges two hashes like Rails.

Ideally, the API and this library could use the same method to merge parameters. This library’s method just needs to be deterministic and not collide distinct combinations.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jahuty/util.rb', line 13

def self.deep_merge(first_hash, other_hash, &block)
  first_hash.merge!(other_hash) do |key, first_val, other_val|
    if first_val.is_a?(Hash) && other_val.is_a?(Hash)
      deep_merge(first_val, other_val, &block)
    elsif block
      yield(key, first_val, other_val)
    else
      other_val
    end
  end
end