Sha256: da0ae8fb6e1180965c1b5d59c31bee5f19ae6a16985e4b33fc5642ad38d841e5

Contents?: true

Size: 1.52 KB

Versions: 10

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

# Patch to hash
class Hash
  # activesupport-5.2.0/lib/active_support/core_ext/hash/deep_merge.rb
  def deep_merge(other_hash, &block)
    dup.deep_merge!(other_hash, &block)
  end

  # Same as +deep_merge+, but modifies +self+.
  def deep_merge!(other_hash, &block)
    merge!(other_hash) do |key, this_val, other_val|
      if this_val.is_a?(Hash) && other_val.is_a?(Hash)
        this_val.deep_merge(other_val, &block)
      elsif block
        block.call(key, this_val, other_val)
      else
        other_val
      end
    end
  end

  def deep_stringify_keys
    deep_transform_keys(&:to_s)
  end

  # activesupport-4.1.1/lib/active_support/core_ext/hash/keys.rb
  def deep_symbolize_keys
    deep_transform_keys do |key|
      key.to_sym
    rescue
      key
    end
  end

  def deep_transform_keys(&block)
    result = {}
    each do |key, value|
      result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value
    end
    result
  end

  # activesupport/lib/active_support/core_ext/hash/deep_transform_values.rb
  def deep_transform_values(&block)
    _deep_transform_values_in_object(self, &block)
  end

  private

  # Support methods for deep transforming nested hashes and arrays.
  def _deep_transform_values_in_object(object, &block)
    case object
    when Hash
      object.transform_values { |value| _deep_transform_values_in_object(value, &block) }
    when Array
      object.map { |e| _deep_transform_values_in_object(e, &block) }
    else
      yield(object)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fusuma-3.7.0 lib/fusuma/hash_support.rb
fusuma-3.6.2 lib/fusuma/hash_support.rb
fusuma-3.6.1 lib/fusuma/hash_support.rb
fusuma-3.6.0 lib/fusuma/hash_support.rb
fusuma-3.5.0 lib/fusuma/hash_support.rb
fusuma-3.4.0 lib/fusuma/hash_support.rb
fusuma-3.3.1 lib/fusuma/hash_support.rb
fusuma-3.3.0 lib/fusuma/hash_support.rb
fusuma-3.2.0 lib/fusuma/hash_support.rb
fusuma-3.1.0 lib/fusuma/hash_support.rb