Class: HashFilterer::Rule
- Inherits:
-
Object
- Object
- HashFilterer::Rule
- Defined in:
- lib/hash_filterer.rb
Overview
Corresponds to a single rule
Defined Under Namespace
Classes: InvalidOperator, InvalidPreprocessor
Instance Attribute Summary collapse
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
Class Method Summary collapse
Instance Method Summary collapse
- #accept?(hash) ⇒ Boolean
-
#initialize(key, operator, value, nil_ok, preprocessor, at_least_one) ⇒ Rule
constructor
nil_ok should be true or false to specify the behavior when the value is nil.
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_message ⇒ Object (readonly)
Returns the value of attribute error_message.
10 11 12 |
# File 'lib/hash_filterer.rb', line 10 def end |
Class Method Details
.allowed_operators ⇒ Object
12 13 14 |
# File 'lib/hash_filterer.rb', line 12 def self.allowed_operators ['==', '!=', '>', '<', 'IN', '=~'] end |
.allowed_preprocessors ⇒ Object
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
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| 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? = msgs.join ' and ' false end |