Module: AngryHash::Merging

Included in:
AngryHash
Defined in:
lib/angry_hash/merging.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
# File 'lib/angry_hash/merging.rb', line 3

def self.included(base)
  base.class_eval do
    alias_method :regular_merge, :merge unless method_defined?(:regular_merge)
  end
end

Instance Method Details

#deep_merge(other_hash) ⇒ Object

Merge deeply. The other hash’s contents are favoured.



26
27
28
29
30
31
32
33
34
35
# File 'lib/angry_hash/merging.rb', line 26

def deep_merge(other_hash)
  other_hash = AngryHash[other_hash]

  self.regular_merge( other_hash ) do |key, oldval, newval|
    oldval = __convert_value(oldval)
    newval = __convert_value(newval)

    AngryHash === oldval && AngryHash === newval ? oldval.deep_merge(newval) : newval
  end
end

#deep_merge!(other_hash) ⇒ Object Also known as: deep_update

Merge deeply, replacing the AngryHash with the result.



38
39
40
41
# File 'lib/angry_hash/merging.rb', line 38

def deep_merge!(other_hash)
  replace(deep_merge(other_hash))
  self
end

#merge(hash) ⇒ Object

Shallow merge hash.



13
14
15
# File 'lib/angry_hash/merging.rb', line 13

def merge(hash)
  super(__convert_without_dup(hash))
end

#merge!(other_hash) ⇒ Object Also known as: update

Shallow merge! hash.



18
19
20
21
# File 'lib/angry_hash/merging.rb', line 18

def merge!(other_hash)
  other_hash.each_pair { |key, value| dup_and_store(key,value) }
  self
end

#reverse_deep_merge(other_hash) ⇒ Object

Merge deeply in reverse. This hash’s contents are favoured.



45
46
47
# File 'lib/angry_hash/merging.rb', line 45

def reverse_deep_merge(other_hash)
  __convert(other_hash).deep_merge(self)
end

#reverse_deep_merge!(other_hash) ⇒ Object Also known as: reverse_deep_update

Merge deeply in reverse, replacing this AngryHash with the result. This hash’s contents are favoured.



50
51
52
53
# File 'lib/angry_hash/merging.rb', line 50

def reverse_deep_merge!(other_hash)
  replace(reverse_deep_merge(other_hash))
  self
end