Sha256: ab03bce98457d373366301892062b5ce97cf0986d6ee70c0341116d431be97a5

Contents?: true

Size: 1015 Bytes

Versions: 6

Compression:

Stored size: 1015 Bytes

Contents

# frozen_string_literal: true

# Invoca ::HashWithIndifferentAccess extensions
if defined? HashWithIndifferentAccess

  class HashWithIndifferentAccess

    # rubocop:disable Naming/BinaryOperatorParameterName
    def -(keys)
      res = HashWithIndifferentAccess.new
      keys = keys.map { |k| k.is_a?(Symbol) ? k.to_s : k }
      each_pair { |k, v| res[k] = v unless k.in?(keys) }
      res
    end

    def &(keys)
      res = HashWithIndifferentAccess.new
      keys.each do |k|
        k = k.to_s if k.is_a?(Symbol)
        res[k] = self[k] if has_key?(k)
      end
      res
    end
    # rubocop:enable Naming/BinaryOperatorParameterName

    def partition_hash(keys = nil)
      keys = keys&.map { |k| k.is_a?(Symbol) ? k.to_s : k }
      yes = HashWithIndifferentAccess.new
      no = HashWithIndifferentAccess.new
      each do |k, v|
        if block_given? ? yield(k, v) : keys.include?(k)
          yes[k] = v
        else
          no[k] = v
        end
      end
      [yes, no]
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
invoca-utils-0.6.0 lib/invoca/utils/hash_with_indifferent_access.rb
invoca-utils-0.5.1 lib/invoca/utils/hash_with_indifferent_access.rb
invoca-utils-0.5.0 lib/invoca/utils/hash_with_indifferent_access.rb
invoca-utils-0.4.1 lib/invoca/utils/hash_with_indifferent_access.rb
invoca-utils-0.4.0 lib/invoca/utils/hash_with_indifferent_access.rb
invoca-utils-0.3.0 lib/invoca/utils/hash_with_indifferent_access.rb