Sha256: b7c6f14ec66e3a4a6cedc6d64c22ee5ba5838afef2e78bf6030060fc76f2262d

Contents?: true

Size: 968 Bytes

Versions: 1

Compression:

Stored size: 968 Bytes

Contents

module Basquiat
  module HashRefinements
    # @!method deep_merge
    #   Merges self with other_hash recursively
    #   @param other_hash [Hash] hash to be merged into self
    #   @return [self]

    # @!method symbolize_keys
    #   Symbolize all the keys in a given hash. Works with nested hashes
    #   @return [Hash] return other hash with the symbolized keys

    refine Hash do
      def deep_merge(other_hash)
        other_hash.each_pair do |key, value|
          current = self[key]
          if current.is_a?(Hash) && value.is_a?(Hash)
            current.deep_merge(value)
          else
            self[key] = value
          end
        end
        self
      end

      def symbolize_keys
        each_with_object({}) do |(key, value), new_hash|
          new_key = key.to_sym rescue key
          new_value         = (value.is_a? Hash) ? value.symbolize_keys : value
          new_hash[new_key] = new_value
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
basquiat-1.3.0.pre.1 lib/basquiat/support/hash_refinements.rb