Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/processengine/HashEnhancement.rb

Instance Method Summary collapse

Instance Method Details

#deep_merge(second) ⇒ Object



40
41
42
43
# File 'lib/processengine/HashEnhancement.rb', line 40

def deep_merge(second)
   merger = proc { |key,v1,v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
   self.merge(second, &merger)
end

#deep_merge!(second) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/processengine/HashEnhancement.rb', line 18

def deep_merge!(second)
  second.each_pair do |k,v|
    if self[k].is_a?(Hash) and second[k].is_a?(Hash)
      self[k].deep_merge!(second[k])
    else
      self[k] = second[k]
    end
  end
end

#deep_merge0(hash) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/processengine/HashEnhancement.rb', line 3

def deep_merge0(hash)
  target = dup

  hash.keys.each do |key|
    if hash[key].is_a? Hash and self[key].is_a? Hash
      target[key] = target[key].deep_merge(hash[key])
      next
    end

   target[key] = hash[key]
  end

  target
end

#deep_merge2(other) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/processengine/HashEnhancement.rb', line 29

def deep_merge2(other)
  deep_proc = Proc.new { |k, s, o|
      if s.kind_of?(Hash) && o.kind_of?(Hash)
         next s.merge(o, &deep_proc)
      end
      next o
   }
   merge(other, &deep_proc)
end

#keep_merge(hash) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/processengine/HashEnhancement.rb', line 46

def keep_merge(hash)
   target = dup
   hash.keys.each do |key|
      if hash[key].is_a? Hash and self[key].is_a? Hash
         target[key] = target[key].keep_merge(hash[key])
         next
      end
      #target[key] = hash[key]
      target.update(hash) { |key, *values| values.flatten.uniq }
   end
   target
end