Class: Odin::Types::SchemaConditional

Inherits:
Object
  • Object
show all
Defined in:
lib/odin/types/schema.rb

Overview

Conditional constraint (when/unless guard)

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fieldObject (readonly)

Returns the value of attribute field.



192
193
194
# File 'lib/odin/types/schema.rb', line 192

def field
  @field
end

#operatorObject (readonly)

Returns the value of attribute operator.



192
193
194
# File 'lib/odin/types/schema.rb', line 192

def operator
  @operator
end

#unlessObject (readonly)

Returns the value of attribute unless.



192
193
194
# File 'lib/odin/types/schema.rb', line 192

def unless
  @unless
end

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