Sha256: 7f5033c572295e58743e4f7ebd9d215142e98b8a53cec668d5782cd110bb7676

Contents?: true

Size: 1.18 KB

Versions: 13

Compression:

Stored size: 1.18 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 |_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

13 entries across 13 versions & 2 rubygems

Version Path
discourse_image_optim-0.26.2 lib/image_optim/hash_helpers.rb
discourse_image_optim-0.26.1 lib/image_optim/hash_helpers.rb
image_optim-0.26.1 lib/image_optim/hash_helpers.rb
image_optim-0.26.0 lib/image_optim/hash_helpers.rb
image_optim-0.25.0 lib/image_optim/hash_helpers.rb
discourse_image_optim-0.24.5 lib/image_optim/hash_helpers.rb
image_optim-0.24.3 lib/image_optim/hash_helpers.rb
discourse_image_optim-0.24.4 lib/image_optim/hash_helpers.rb
image_optim-0.24.2 lib/image_optim/hash_helpers.rb
image_optim-0.24.1 lib/image_optim/hash_helpers.rb
image_optim-0.24.0 lib/image_optim/hash_helpers.rb
image_optim-0.23.0 lib/image_optim/hash_helpers.rb
image_optim-0.22.1 lib/image_optim/hash_helpers.rb