module ImageOptim::HashHelpers

Helper methods to manipulate Hash, mainly used in config

Public Class Methods

deep_merge(hash_a, hash_b) click to toggle source

Returns a new hash with recursive merge of all keys

# File lib/image_optim/hash_helpers.rb, line 20
def deep_merge(hash_a, hash_b)
  hash_a.merge(hash_b) do |_key, value_a, value_b|
    if value_a.is_a?(Hash) && value_b.is_a?(Hash)
      deep_merge(value_a, value_b)
    else
      value_b
    end
  end
end
deep_stringify_keys(hash) click to toggle source

Returns a new hash with all keys of root and nested hashes converted to strings

# File lib/image_optim/hash_helpers.rb, line 9
def deep_stringify_keys(hash)
  deep_transform_keys(hash, &:to_s)
end
deep_symbolise_keys(hash) click to toggle source

Returns a new hash with all keys of root and nested hashes converted to symbols

# File lib/image_optim/hash_helpers.rb, line 15
def deep_symbolise_keys(hash)
  deep_transform_keys(hash, &:to_sym)
end

Private Class Methods

deep_transform_keys(hash) { |key] =| ... } click to toggle source

Returns a new hash with all keys of root and nested hashes converted by provided block

# File lib/image_optim/hash_helpers.rb, line 34
def deep_transform_keys(hash, &block)
  new_hash = {}
  hash.each do |key, value|
    new_hash[yield key] = if value.is_a?(Hash)
      deep_transform_keys(value, &block)
    else
      value
    end
  end