Sha256: 4222ba3806291e3097d4c9c7a46b79170b585d2c9d3c7e08c928c2dc5e8cec62

Contents?: true

Size: 842 Bytes

Versions: 6

Compression:

Stored size: 842 Bytes

Contents

module Furoshiki
  module Util
    # Ensure symbol keys, even in nested hashes
    #
    # @param [Hash] config the hash to set (key: value) on
    # @param [#to_sym] k the key
    # @param [Object] v the value
    # @return [Hash] an updated hash
    def deep_set_symbol_key(hash, key, value)
      if value.kind_of? Hash
        hash[key.to_sym] = value.inject({}) { |inner_hash, (inner_key, inner_value)| deep_set_symbol_key(inner_hash, inner_key, inner_value) }
      else
        hash[key.to_sym] = value
      end
      hash
    end

    def deep_symbolize_keys(hash)
      merge_with_symbolized_keys({}, hash)
    end

    # Assumes that defaults already has symbolized keys
    def merge_with_symbolized_keys(defaults, hash)
      hash.inject(defaults) { |symbolized, (k, v)| deep_set_symbol_key(symbolized, k, v) }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
furoshiki-0.6.1 lib/furoshiki/util.rb
furoshiki-0.6.0 lib/furoshiki/util.rb
furoshiki-0.5.0 lib/furoshiki/util.rb
furoshiki-0.4.0 lib/furoshiki/util.rb
furoshiki-0.3.1 lib/furoshiki/util.rb
furoshiki-0.3.0 lib/furoshiki/util.rb