Sha256: 06e124e1a63be974ea0ee81adc8331791d311d544b9fb67c54d64e0d761129d6

Contents?: true

Size: 1.96 KB

Versions: 13

Compression:

Stored size: 1.96 KB

Contents

class Hash

  # 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!
  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
  
  # 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:
  # {:foo=>{:bar=>"baz", :car=>"naz"}, :bar=>"baz"} - {:foo=>{:car=>"naz"}} == {:foo=>{:bar=>"baz"}, :bar=>"baz"}
  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
  
  # 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.
  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
    
  # Merges self with another hash, recursively.
  # 
  # This code was lovingly stolen from some random gem:
  # http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
  # 
  # Thanks to whoever made it.
  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
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
videojuicer-player-sdk-0.2 lib/playersdk/core_ext/hash.rb
videojuicer-player-sdk-0.3.5 lib/playersdk/core_ext/hash.rb
videojuicer-player-sdk-0.3.6 lib/playersdk/core_ext/hash.rb
videojuicer-player-sdk-0.3.9 lib/playersdk/core_ext/hash.rb
videojuicer-player-sdk-0.4.0 lib/playersdk/core_ext/hash.rb
videojuicer-player-sdk-0.4.1 lib/playersdk/core_ext/hash.rb
videojuicer-player-sdk-0.4.3 lib/playersdk/core_ext/hash.rb
stack-0.0.9 lib/core_ext/hash.rb
stack-0.0.8 lib/core_ext/hash.rb
stack-0.0.7 lib/core_ext/hash.rb
stack-0.0.3 lib/core_ext/hash.rb
stack-0.0.2 lib/core_ext/hash.rb
vj-player-sdk-0.4.5 lib/playersdk/core_ext/hash.rb