Sha256: 920de6ab5b97aabfd72745d36340c87df5f51e949059e81ce4ff67d0d303d834

Contents?: true

Size: 828 Bytes

Versions: 11

Compression:

Stored size: 828 Bytes

Contents

##
# Various Hash extensions.
#
class Hash

  # Recursively merge +other_hash+ into +self+ and return the new hash.
  def deep_merge(other_hash)
    self.merge(other_hash) do |key, oldval, newval|
      oldval = oldval.to_hash if oldval.respond_to?(:to_hash) && !oldval.is_a?(Array)
      newval = newval.to_hash if newval.respond_to?(:to_hash) && !newval.is_a?(Array)
      oldval.is_a?(Hash) && newval.is_a?(Hash) ? oldval.deep_merge(newval) : newval
    end
  end

  # Recursively merge and replace +other_hash+ into +self+.
  def deep_merge!(other_hash)
    replace(deep_merge(other_hash))
  end

  # accumulate existing keys from +other_hash+ into +self+.
  def delta_merge!(other_hash)
    other_hash.each do |k,v|
      if self.has_key?(k)
        self[k] += v
      else
        self[k] = v
      end
    end
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
liquid-ext-3.5.0 lib/liquid/ext/hash.rb
liquid-ext-3.4.2 lib/liquid/ext/hash.rb
liquid-ext-3.4.1 lib/liquid/ext/hash.rb
liquid-ext-3.4.0 lib/liquid/ext/hash.rb
liquid-ext-3.3.10 lib/liquid/ext/hash.rb
liquid-ext-3.3.9 lib/liquid/ext/hash.rb
liquid-ext-3.3.8 lib/liquid/ext/hash.rb
liquid-ext-3.3.7 lib/liquid/ext/hash.rb
liquid-ext-3.3.6 lib/liquid/ext/hash.rb
liquid-ext-3.3.5 lib/liquid/ext/hash.rb
liquid-ext-3.3.4 lib/liquid/ext/hash.rb