Class: Core::Filtering::SimpleRule

Inherits:
Object
  • Object
show all
Defined in:
lib/core/filtering/simple_rules/simple_rule.rb

Defined Under Namespace

Classes: Policy, Rule

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

Class Method Summary collapse

Instance Method Summary collapse

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

#fieldObject (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

#idObject (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

#policyObject (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

#ruleObject (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

#valueObject (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.message}")
  value.to_s
end

#is_exclude?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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_hObject



137
138
139
# File 'lib/core/filtering/simple_rules/simple_rule.rb', line 137

def to_h
  @rule_hash
end

#try_coerce_valueObject



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