Sha256: c87def10ca6a21fd56d60afe8ad9c51ed07ee007ca281b4975c4c03bd2cb4b02

Contents?: true

Size: 998 Bytes

Versions: 3

Compression:

Stored size: 998 Bytes

Contents

# frozen_string_literal: true
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

3 entries across 3 versions & 1 rubygems

Version Path
basquiat-1.3.2 lib/basquiat/support/hash_refinements.rb
basquiat-1.3.1 lib/basquiat/support/hash_refinements.rb
basquiat-1.3.0 lib/basquiat/support/hash_refinements.rb