Sha256: c9677fc8d4f9fd228852bcacc802dc4fa019ee3fa0af694eca67ee78c24a4b2d

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Refinements
  module Hashes
    refine Hash do
      def except *keys
        reject { |key, _value| keys.include? key }
      end

      def except! *keys
        replace except(*keys)
      end

      def symbolize_keys
        reduce({}) { |hash, (key, value)| hash.merge key.to_sym => value }
      end

      def symbolize_keys!
        replace symbolize_keys
      end

      def deep_merge other
        dup.deep_merge! other
      end

      # :reek:FeatureEnvy
      def deep_merge! other
        merge! other do |_key, this_value, other_value|
          if this_value.is_a?(Hash) && other_value.is_a?(Hash)
            this_value.deep_merge other_value
          else
            other_value
          end
        end
      end

      def reverse_merge other
        other.merge self
      end

      def reverse_merge! other
        merge!(other) { |_key, old_value, _new_value| old_value }
      end

      def use &block
        return [] unless block_given?

        block.parameters
             .map { |(_type, key)| self[key] }
             .then { |values| yield values }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
refinements-7.3.0 lib/refinements/hashes.rb
refinements-7.2.0 lib/refinements/hashes.rb