Class: Odin::Types::SchemaConditional
- Inherits:
-
Object
- Object
- Odin::Types::SchemaConditional
- Defined in:
- lib/odin/types/schema.rb
Overview
Conditional constraint (when/unless guard)
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#unless ⇒ Object
readonly
Returns the value of attribute unless.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #evaluate(doc_value) ⇒ Object
-
#initialize(field:, operator: "=", value: nil, unless_cond: false) ⇒ SchemaConditional
constructor
A new instance of SchemaConditional.
Constructor Details
#initialize(field:, operator: "=", value: nil, unless_cond: false) ⇒ SchemaConditional
Returns a new instance of SchemaConditional.
194 195 196 197 198 199 200 |
# File 'lib/odin/types/schema.rb', line 194 def initialize(field:, operator: "=", value: nil, unless_cond: false) @field = field.freeze @operator = operator.freeze @value = value @unless = unless_cond freeze end |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
192 193 194 |
# File 'lib/odin/types/schema.rb', line 192 def field @field end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
192 193 194 |
# File 'lib/odin/types/schema.rb', line 192 def operator @operator end |
#unless ⇒ Object (readonly)
Returns the value of attribute unless.
192 193 194 |
# File 'lib/odin/types/schema.rb', line 192 def unless @unless end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
192 193 194 |
# File 'lib/odin/types/schema.rb', line 192 def value @value end |
Instance Method Details
#evaluate(doc_value) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/odin/types/schema.rb', line 202 def evaluate(doc_value) result = case @operator when "=" then doc_value.to_s == @value.to_s when "!=" then doc_value.to_s != @value.to_s when ">" then numeric_compare(doc_value, @value) { |a, b| a > b } when "<" then numeric_compare(doc_value, @value) { |a, b| a < b } when ">=" then numeric_compare(doc_value, @value) { |a, b| a >= b } when "<=" then numeric_compare(doc_value, @value) { |a, b| a <= b } else false end @unless ? !result : result end |