Class: Core::Filtering::SimpleRule
- Inherits:
-
Object
- Object
- Core::Filtering::SimpleRule
- Defined in:
- lib/core/filtering/simple_rules/simple_rule.rb
Defined Under Namespace
Constant Summary collapse
- DEFAULT_RULE_ID =
'DEFAULT'
- DEFAULT_RULE =
SimpleRule.new( 'policy' => 'include', 'field' => '_', 'rule' => 'regex', 'value' => '.*', 'id' => SimpleRule::DEFAULT_RULE_ID )
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#policy ⇒ Object
readonly
Returns the value of attribute policy.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #coerce(doc_value) ⇒ Object
-
#initialize(rule_hash) ⇒ SimpleRule
constructor
A new instance of SimpleRule.
- #is_exclude? ⇒ Boolean
- #is_include? ⇒ Boolean
- #match?(document) ⇒ Boolean
- #to_h ⇒ Object
- #try_coerce_value ⇒ Object
Constructor Details
#initialize(rule_hash) ⇒ SimpleRule
Returns a new instance of SimpleRule.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 48 def initialize(rule_hash) @policy = rule_hash.fetch('policy') @field = rule_hash.fetch('field') @rule = rule_hash.fetch('rule') @value = rule_hash.fetch('value') @id = rule_hash.fetch('id') @rule_hash = rule_hash rescue KeyError => e raise "#{e.key} is required" end |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
46 47 48 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 46 def field @field end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
46 47 48 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 46 def id @id end |
#policy ⇒ Object (readonly)
Returns the value of attribute policy.
46 47 48 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 46 def policy @policy end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule.
46 47 48 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 46 def rule @rule end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
46 47 48 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 46 def value @value end |
Class Method Details
.from_args(id, policy, field, rule, value) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 59 def self.from_args(id, policy, field, rule, value) SimpleRule.new( { 'id' => id, 'policy' => policy, 'field' => field, 'rule' => rule, 'value' => value } ) end |
Instance Method Details
#coerce(doc_value) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 111 def coerce(doc_value) case doc_value when String value.to_s when Integer value.to_i when DateTime, Time to_date(value) when TrueClass, FalseClass # Ruby doesn't have a Boolean type, TIL to_bool(value).to_s else value.to_s end rescue StandardError => e Utility::Logger.debug("Failed to coerce value '#{value}' (#{value.class}) based on document value '#{doc_value}' (#{doc_value.class}) due to error: #{e.class}: #{e.}") value.to_s end |
#is_exclude? ⇒ Boolean
133 134 135 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 133 def is_exclude? policy == Policy::EXCLUDE end |
#is_include? ⇒ Boolean
129 130 131 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 129 def is_include? policy == Policy::INCLUDE end |
#match?(document) ⇒ Boolean
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 79 def match?(document) return true if id == DEFAULT_RULE_ID doc_value = document[field] return false if doc_value.nil? coerced_value = coerce(doc_value) case rule when Rule::EQUALS case coerced_value when Integer doc_value == coerced_value when DateTime, Time doc_value.to_s == coerced_value.to_s else doc_value.to_s == coerced_value end when Rule::STARTS_WITH doc_value.to_s.start_with?(value) when Rule::ENDS_WITH doc_value.to_s.end_with?(value) when Rule::CONTAINS doc_value.to_s.include?(value) when Rule::REGEX doc_value.to_s.match(/#{value}/) when Rule::LESS_THAN doc_value < coerced_value when Rule::GREATER_THAN doc_value > coerced_value else false end end |
#to_h ⇒ Object
137 138 139 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 137 def to_h @rule_hash end |
#try_coerce_value ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 141 def try_coerce_value coerced = to_float(value) begin coerced = to_date(value) if coerced.is_a?(String) rescue ArgumentError coerced = to_bool(value) if coerced.is_a?(String) end coerced rescue StandardError value end |