Class: HashWithIndifferentAccess

Inherits:
Object
  • Object
show all
Defined in:
lib/invoca/utils/hash_with_indifferent_access.rb

Instance Method Summary collapse

Instance Method Details

#&(keys) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/invoca/utils/hash_with_indifferent_access.rb', line 16

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

#-(keys) ⇒ Object

rubocop:disable Naming/BinaryOperatorParameterName



9
10
11
12
13
14
# File 'lib/invoca/utils/hash_with_indifferent_access.rb', line 9

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

#partition_hash(keys = nil) ⇒ Object

rubocop:enable Naming/BinaryOperatorParameterName



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/invoca/utils/hash_with_indifferent_access.rb', line 26

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