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.



200
201
202
203
204
205
206
# File 'lib/odin/types/schema.rb', line 200

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.



198
199
200
# File 'lib/odin/types/schema.rb', line 198

def field
  @field
end

#operatorObject (readonly)

Returns the value of attribute operator.



198
199
200
# File 'lib/odin/types/schema.rb', line 198

def operator
  @operator
end

#unlessObject (readonly)

Returns the value of attribute unless.



198
199
200
# File 'lib/odin/types/schema.rb', line 198

def unless
  @unless
end

#valueObject (readonly)

Returns the value of attribute value.



198
199
200
# File 'lib/odin/types/schema.rb', line 198

def value
  @value
end

Instance Method Details

#evaluate(doc_value) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/odin/types/schema.rb', line 208

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