Sha256: eb66d9ad27649a093518d6f69e40bc8299ec1cb2f738fd2a4717671f2e3ec427

Contents?: true

Size: 1.14 KB

Versions: 8

Compression:

Stored size: 1.14 KB

Contents

require 'forwardable'

module Esse
  # The idea here is to add useful methods to the ruby standard objects without
  # monkey patching them
  module HashUtils
    module_function

    def deep_dup(hash)
      hash.each_with_object({}) do |(key, value), result|
        result[key] = \
          if value.is_a?(Hash)
            deep_dup(value)
          else
            value
          end
      end
    end

    def deep_transform_keys(hash, &block)
      hash.each_with_object({}) do |(key, value), result|
        result[yield(key)] = \
          if value.is_a?(Hash)
            deep_transform_keys(value, &block)
          else
            value
          end
      end
    end

    def deep_merge(target, source)
      target.merge(source) do |key, oldval, newval|
        if oldval.is_a?(Hash) && newval.is_a?(Hash)
          deep_merge(oldval, newval)
        else
          newval
        end
      end
    end

    def deep_merge!(target, source)
      target.merge!(source) do |key, oldval, newval|
        if oldval.is_a?(Hash) && newval.is_a?(Hash)
          deep_merge!(oldval, newval)
        else
          newval
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
esse-0.3.4 lib/esse/primitives/hash_utils.rb
esse-0.3.3 lib/esse/primitives/hash_utils.rb
esse-0.3.2 lib/esse/primitives/hash_utils.rb
esse-0.3.1 lib/esse/primitives/hash_utils.rb
esse-0.3.0 lib/esse/primitives/hash_utils.rb
esse-0.2.6 lib/esse/primitives/hash_utils.rb
esse-0.2.5 lib/esse/primitives/hash_utils.rb
esse-0.2.4 lib/esse/primitives/hash_utils.rb