Sha256: 6a2f13a243daed82c82cda2eb8b6617be501a01b385763779fb05f1406f04fa3

Contents?: true

Size: 941 Bytes

Versions: 5

Compression:

Stored size: 941 Bytes

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_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

5 entries across 5 versions & 1 rubygems

Version Path
esse-0.2.3 lib/esse/primitives/hash_utils.rb
esse-0.2.2 lib/esse/primitives/hash_utils.rb
esse-0.2.0 lib/esse/primitives/hash_utils.rb
esse-0.1.3 lib/esse/primitives/hash_utils.rb
esse-0.1.2 lib/esse/primitives/hash_utils.rb