Class: VerbotenKeys::Filterer

Inherits:
Object
  • Object
show all
Defined in:
lib/verboten_keys/filterer.rb

Class Method Summary collapse

Class Method Details

.evaluate_key_value_pair(key, value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/verboten_keys/filterer.rb', line 11

def self.evaluate_key_value_pair(key, value)
  key = key.to_sym

  if VerbotenKeys.configuration.forbidden_keys.include?(key)
    case VerbotenKeys.configuration.strategy
    when :remove
      return [nil, nil]
    when :nullify
      return [key, nil]
    else
      raise VerbotenKeys::Errors::StrategyNotFoundError(VerbotenKeys.configuration.strategy)
    end
  end

  case value
  when Hash
    return [key, VerbotenKeys::Filterer.filter_forbidden_keys(value)]
  when Array
    return [key, value.map { |item| item.is_a?(Hash) ? VerbotenKeys::Filterer.filter_forbidden_keys(item) : item }]
  end

  [key, value]
end

.filter_forbidden_keys(hash) ⇒ Object



5
6
7
8
9
# File 'lib/verboten_keys/filterer.rb', line 5

def self.filter_forbidden_keys(hash)
  hash
    .to_h { |k, v| evaluate_key_value_pair(k, v) }
    .delete_if { |k, _v| k.nil? }
end