Sha256: 7b76823e984c53d8eda3f5709246c29fea607beb8e0939d98387507aa8060862

Contents?: true

Size: 908 Bytes

Versions: 23

Compression:

Stored size: 908 Bytes

Contents

# :nodoc:
module Prmd
  # Hash helper methods
  #
  # @api private
  module HashHelpers
    # Attempts to convert all keys in the hash to a Symbol.
    # This operation is recursive with subhashes
    #
    # @param [Hash] hash
    # @return [Hash]
    def self.deep_symbolize_keys(hash)
      deep_transform_keys(hash) do |key|
        if key.respond_to?(:to_sym)
          key.to_sym
        else
          key
        end
      end
    end

    # Think of this as hash.keys.map! { |key| }, that actually maps recursively.
    #
    # @param [Hash] hash
    # @return [Hash]
    # @yield [Object] key
    def self.deep_transform_keys(hash, &block)
      result = {}
      hash.each do |key, value|
        new_key = yield(key)
        new_value = value
        new_value = deep_transform_keys(value, &block) if value.is_a?(Hash)
        result[new_key] = new_value
      end
      result
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
prmd-0.14.0 lib/prmd/hash_helpers.rb
prmd-0.13.0 lib/prmd/hash_helpers.rb
prmd-0.12.0 lib/prmd/hash_helpers.rb
prmd-0.11.11 lib/prmd/hash_helpers.rb
prmd-0.11.10 lib/prmd/hash_helpers.rb
prmd-0.11.9 lib/prmd/hash_helpers.rb
prmd-0.11.8 lib/prmd/hash_helpers.rb
prmd-0.11.7 lib/prmd/hash_helpers.rb
prmd-0.11.6 lib/prmd/hash_helpers.rb
prmd-0.11.5 lib/prmd/hash_helpers.rb
prmd-0.11.4 lib/prmd/hash_helpers.rb
prmd-0.11.3 lib/prmd/hash_helpers.rb
prmd-0.11.2 lib/prmd/hash_helpers.rb
prmd-0.11.1 lib/prmd/hash_helpers.rb
prmd-0.11.0 lib/prmd/hash_helpers.rb
prmd-0.10.0 lib/prmd/hash_helpers.rb
prmd-0.9.0 lib/prmd/hash_helpers.rb
prmd-0.8.0 lib/prmd/hash_helpers.rb
prmd-0.7.4 lib/prmd/hash_helpers.rb
prmd-0.7.3 lib/prmd/hash_helpers.rb