Sha256: 16728844c37d1f98eb600894263a34869806930ac4f0e332aa310669af12140f

Contents?: true

Size: 1.08 KB

Versions: 8

Compression:

Stored size: 1.08 KB

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 = begin
                      key.to_sym
                    rescue StandardError
                      key
                    end
          new_value         = value.is_a?(Hash) ? value.symbolize_keys : value
          new_hash[new_key] = new_value
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
basquiat-1.6.0 lib/basquiat/support/hash_refinements.rb
basquiat-1.5.2 lib/basquiat/support/hash_refinements.rb
basquiat-1.5.1 lib/basquiat/support/hash_refinements.rb
basquiat-1.5.0 lib/basquiat/support/hash_refinements.rb
basquiat-1.4.0 lib/basquiat/support/hash_refinements.rb
basquiat-1.3.6 lib/basquiat/support/hash_refinements.rb
basquiat-1.3.5 lib/basquiat/support/hash_refinements.rb
basquiat-1.3.4 lib/basquiat/support/hash_refinements.rb