Class: HashFilterer::Rule

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

Overview

Corresponds to a single rule

Defined Under Namespace

Classes: InvalidOperator, InvalidPreprocessor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, operator, value, nil_ok, preprocessor, at_least_one) ⇒ Rule

nil_ok should be true or false to specify the behavior when the value is nil



22
23
24
25
26
27
28
29
30
31
# File 'lib/hash_filterer.rb', line 22

def initialize(key, operator, value, nil_ok, preprocessor, at_least_one) # rubocop:disable Metrics/ParameterLists
  @keys = Array.wrap key
  @operator = operator
  @value = value
  @nil_ok = nil_ok || false
  @preprocessors = Array.wrap preprocessor
  @at_least_one = at_least_one || false
  check_operator!
  check_preprocessor!
end

Instance Attribute Details

#error_messageObject (readonly)

Returns the value of attribute error_message.



10
11
12
# File 'lib/hash_filterer.rb', line 10

def error_message
  @error_message
end

Class Method Details

.allowed_operatorsObject



12
13
14
# File 'lib/hash_filterer.rb', line 12

def self.allowed_operators
  ['==', '!=', '>', '<', 'IN', '=~']
end

.allowed_preprocessorsObject



16
17
18
19
# File 'lib/hash_filterer.rb', line 16

def self.allowed_preprocessors
  # TODO: Make this a config / maybe just remove
  ['downcase', 'upcase', 'nil?', 'blank?', 'to_s', 'to_f', 'to_i', 'strip', 'count']
end

Instance Method Details

#accept?(hash) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
# File 'lib/hash_filterer.rb', line 33

def accept?(hash)
  values = read_values hash
  msgs = values.map do |actual|
    build_message actual unless ok_for actual
  end.compact
  return true if @at_least_one && values.length > msgs.length
  return true if !@at_least_one && msgs.empty?
  @error_message = msgs.join ' and '
  false
end