Class: Pal::Operation::OperatorRule

Inherits:
Rule
  • Object
show all
Defined in:
lib/pal/operation/filter_evaluator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Rule

#evaluate

Constructor Details

#initialize(rule_hash) ⇒ OperatorRule

Returns a new instance of OperatorRule.



117
118
119
120
121
122
123
# File 'lib/pal/operation/filter_evaluator.rb', line 117

def initialize(rule_hash)
  super()
  @field = rule_hash.fetch("field")
  @type = rule_hash.fetch("type").to_sym
  @operator = rule_hash.fetch("operator").to_sym
  @comparison_value = rule_hash.fetch("value")
end

Instance Attribute Details

#comparison_valueObject (readonly)

Returns:

  • (Object)


112
113
114
# File 'lib/pal/operation/filter_evaluator.rb', line 112

def comparison_value
  @comparison_value
end

#fieldString (readonly)

Returns:

  • (String)


109
110
111
# File 'lib/pal/operation/filter_evaluator.rb', line 109

def field
  @field
end

#operatorString (readonly)

Returns:

  • (String)


109
110
111
# File 'lib/pal/operation/filter_evaluator.rb', line 109

def operator
  @operator
end

#typeSymbol (readonly)

Returns:

  • (Symbol)


115
116
117
# File 'lib/pal/operation/filter_evaluator.rb', line 115

def type
  @type
end

Instance Method Details

#_evaluate(eval_ctx) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pal/operation/filter_evaluator.rb', line 127

def _evaluate(eval_ctx)
  operator_candidates = get_operators_for_type(@type)

  property = eval_ctx.get_value(@field)
  return false unless property

  proc = operator_candidates.fetch(@operator, proc do |_x, _y|
    raise "Invalid operator given - [#{@operator}]. Valid candidates are [#{operator_candidates.keys.join(", ")}]"
  end)

  converted_comparison = convert_property(@type, @comparison_value)
  converted_property = convert_property(@type, property)

  proc.call(converted_property, converted_comparison)
end

#convert_property(type, prop) ⇒ Object

Parameters:

  • type (Symbol)

Returns:

  • (Object)


162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/pal/operation/filter_evaluator.rb', line 162

def convert_property(type, prop)
  case type
  when :string
    prop.to_s
  when :number
    prop.is_a?(Array) ? prop : prop.to_f
  when :json
    prop
  when :date
    Date.parse(prop)
  else
    raise "Missing property operator for [#{type}], valid candidates: [string, number, json, date]"
  end
end

#get_operators_for_type(type) ⇒ Hash{Symbol->Proc

Returns ].

Parameters:

  • type (Symbol)

Returns:

  • (Hash{Symbol->Proc)

    ]



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pal/operation/filter_evaluator.rb', line 145

def get_operators_for_type(type)
  case type
  when :string
    string_operators
  when :number
    number_operators
  when :date
    date_operators
  when :json
    json_operators
  else
    raise "Missing filter operator for [#{type}], valid candidates: [string, number, tag, date]"
  end
end