Sha256: 3c5448554b527257031f70a4c3108941b48a8ff00c189440b0038045726446a7

Contents?: true

Size: 1.14 KB

Versions: 14

Compression:

Stored size: 1.14 KB

Contents

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(a, b)
        a.merge(b) do |_k, v_a, v_b|
          if v_a.is_a?(Hash) && v_b.is_a?(Hash)
            deep_merge(v_a, v_b)
          else
            v_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 |k, v|
          new_hash[block.call(k)] = if v.is_a?(Hash)
            deep_transform_keys(v, &block)
          else
            v
          end
        end
        new_hash
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
image_optim-0.22.0 lib/image_optim/hash_helpers.rb
openstreetmap-image_optim-0.21.0.1 lib/image_optim/hash_helpers.rb
image_optim-0.21.0 lib/image_optim/hash_helpers.rb
image_optim-0.20.2 lib/image_optim/hash_helpers.rb
image_optim-0.20.1 lib/image_optim/hash_helpers.rb
image_optim-0.20.0 lib/image_optim/hash_helpers.rb
image_optim-0.19.1 lib/image_optim/hash_helpers.rb
image_optim-0.19.0 lib/image_optim/hash_helpers.rb
image_optim-0.18.0 lib/image_optim/hash_helpers.rb
image_optim-0.17.1 lib/image_optim/hash_helpers.rb
image_optim-0.17.0 lib/image_optim/hash_helpers.rb
image_optim-0.16.0 lib/image_optim/hash_helpers.rb
image_optim-0.15.0 lib/image_optim/hash_helpers.rb
image_optim-0.14.0 lib/image_optim/hash_helpers.rb