Sha256: 0f38de943ce8b1fa70576e8fd42e81fd61e268d6febf571f26909a41bdcf5280

Contents?: true

Size: 1.23 KB

Versions: 12

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

class ImageOptim
  # Helper methods to manipulate Hash, mainly used in config
  module HashHelpers
    class << self
      # Returns a new hash with all keys of root and nested hashes converted to
      # strings
      def deep_stringify_keys(hash)
        deep_transform_keys(hash, &:to_s)
      end

      # Returns a new hash with all keys of root and nested hashes converted to
      # symbols
      def deep_symbolise_keys(hash)
        deep_transform_keys(hash, &:to_sym)
      end

      # Returns a new hash with recursive merge of all keys
      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

    private

      # Returns a new hash with all keys of root and nested hashes converted by
      # provided block
      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
        new_hash
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
image_optim-0.31.4 lib/image_optim/hash_helpers.rb
image_optim-0.31.3 lib/image_optim/hash_helpers.rb
image_optim-0.31.2 lib/image_optim/hash_helpers.rb
image_optim-0.31.1 lib/image_optim/hash_helpers.rb
image_optim-0.31.0 lib/image_optim/hash_helpers.rb
image_optim-0.30.0 lib/image_optim/hash_helpers.rb
image_optim-0.29.0 lib/image_optim/hash_helpers.rb
image_optim-0.28.0 lib/image_optim/hash_helpers.rb
image_optim-0.27.1 lib/image_optim/hash_helpers.rb
image_optim-0.27.0 lib/image_optim/hash_helpers.rb
image_optim-0.26.5 lib/image_optim/hash_helpers.rb
image_optim-0.26.4 lib/image_optim/hash_helpers.rb