Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-easyconfig/hash.rb

Overview

-*- mode: ruby -*- vi: set ft=ruby :

Instance Method Summary collapse

Instance Method Details

#deep_merge(second) ⇒ Object



5
6
7
8
# File 'lib/vagrant-easyconfig/hash.rb', line 5

def deep_merge(second)
  merger = proc { |_, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : Array === v1 && Array === v2 ? v1 | v2 : [:undefined, nil, :nil].include?(v2) ? v1 : v2 }
  merge(second.to_h, &merger)
end

#deep_merge!(second) ⇒ Object



9
10
11
12
# File 'lib/vagrant-easyconfig/hash.rb', line 9

def deep_merge!(second)
  merger = proc { |_, v1, v2| Hash === v1 && Hash === v2 ? v1.merge!(v2, &merger) : Array === v1 && Array === v2 ? v1 | v2 : [:undefined, nil, :nil].include?(v2) ? v1 : v2 }
  merge!(second.to_h, &merger)
end

#deep_update!(second) ⇒ Object



13
14
15
16
# File 'lib/vagrant-easyconfig/hash.rb', line 13

def deep_update!(second)
  merger = proc { |_, v1, v2| Hash === v1 && Hash === v2 ? v1.merge!(v2.select { |k| v1.keys.include? k }, &merger) : Array === v1 && Array === v2 ? v1 | v2 : [:undefined, nil, :nil].include?(v2) ? v1 : v2 }
  merge!(second.to_h.select { |k| keys.include? k }, &merger)
end