Sha256: ecde18d698e91df86c5320706b49d2277d9ee166687a973bca082fd96daed2f6

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Refinements
  # Refinements for Hashes.
  module Hashes
    # rubocop:disable Metrics/BlockLength
    refine Hash do
      def except *keys
        reject { |key, _value| keys.include? key }
      end

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

      def symbolize_keys
        dup.symbolize_keys!
      end

      def symbolize_keys!
        keys.each { |key| self[key.to_sym] = delete(key) }
        self
      end

      def slice *keys
        select { |key, _value| keys.include? key }
      end

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

      def deep_merge other
        dup.deep_merge! other
      end

      def deep_merge! other
        other.each do |(other_key, other_value)|
          current_value = self[other_key]

          self[other_key] = if current_value.is_a?(Hash) && other_value.is_a?(Hash)
                              current_value.deep_merge! other_value
                            else
                              other_value
                            end
        end

        self
      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?

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
refinements-4.2.0 lib/refinements/hashes.rb
refinements-4.1.0 lib/refinements/hashes.rb
refinements-4.0.0 lib/refinements/hashes.rb