Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#-(arg) ⇒ Object

Subtracts one hash from another by removing keys from the actor and recursing on any hash values. Returns a new Hash without affecting the actors. Example 1: :foo=>“bar” - :foo=>“bar” == {} Example 2 of deep nesting: :car=>“naz”, :bar=>“baz”} - :foo=>{:car=>“naz”} == :foo=>{:bar=>“baz”, :bar=>“baz”}



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/core_ext/hash.rb', line 21

def -(arg)
  target = dup; actor = arg.dup
  actor.each do |key, value|
    if value.is_a?(Hash)
      target[key] = target[key] - value
    else
      target.delete(key)
    end
  end
  return target
end

#deep_liquify(payload = {}) ⇒ Object

Returns a new hash just like this one, but with all STRING keys and values evaluated and rendered as liquid templates using the provided payload hash.



35
36
37
38
39
40
41
42
43
44
# File 'lib/core_ext/hash.rb', line 35

def deep_liquify(payload={})
  target = dup
  target.inject({}) do |memo, (key, value)|
    value = value.deep_liquify(payload) if value.is_a?(Hash)
    value = value.liquify(payload) if value.is_a?(String)
    key = key.liquify(payload) if key.is_a?(String)
    memo[(key.to_sym rescue key) || key] = value
    memo
  end
end

#deep_merge(hash) ⇒ Object

Merges self with another hash, recursively.

This code was lovingly stolen from some random gem: gemjack.com/gems/tartan-0.1.1/classes/Hash.html

Thanks to whoever made it.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/core_ext/hash.rb', line 52

def deep_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].deep_merge(hash[key])
      next
    end      
    target[key] = hash[key]
  end    
  target
end

#deep_symbolizeObject

Returns a new hash just like this one, but with all the string keys expressed as symbols. Also applies to hashes within self. Based on an implementation within Rails 2.x, thanks Rails!



6
7
8
9
10
11
12
13
# File 'lib/core_ext/hash.rb', line 6

def deep_symbolize
  target = dup    
  target.inject({}) do |memo, (key, value)|
    value = value.deep_symbolize if value.is_a?(Hash)
    memo[(key.to_sym rescue key) || key] = value
    memo
  end
end