Sha256: 2c97e24a6b757e824e3ae621acb2ed124075a956e4d955996d0ec6b2605009a4

Contents?: true

Size: 1005 Bytes

Versions: 2

Compression:

Stored size: 1005 Bytes

Contents

# frozen_string_literal: true

module Refinements
  # Refinements for Hashes.
  module HashExtensions
    refine Hash do
      def compact
        dup.compact!
      end

      def compact!
        reject! { |_, value| value.nil? }
      end

      def deep_merge other_hash
        dup.deep_merge! other_hash
      end

      def deep_merge! other_hash
        other_hash.each.with_object self do |(other_key, other_value), original_hash|
          current_value = original_hash[other_key]
          original_hash[other_key] = deep_merge_value current_value, other_value
        end
      end

      def reverse_merge other_hash
        other_hash.merge self
      end

      def reverse_merge! other_hash
        merge!(other_hash) { |_, left_value, _| left_value }
      end

      private

      def deep_merge_value current_value, other_value
        return current_value.deep_merge(other_value) if current_value.is_a?(Hash) && other_value.is_a?(Hash)
        other_value
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
refinements-2.2.1 lib/refinements/hash_extensions.rb
refinements-2.2.0 lib/refinements/hash_extensions.rb