Class: Datacaster::OrNode

Inherits:
Base
  • Object
show all
Defined in:
lib/datacaster/or_node.rb

Instance Method Summary collapse

Methods included from Mixin

#&, #*, #call, #call_with_runtime, #cast_errors, #i18n_key, #i18n_map_keys, #i18n_scope, #i18n_vars, #json_schema, #json_schema_attributes, #then, #with_context, #with_object_context, #with_runtime, #|

Constructor Details

#initialize(left, right) ⇒ OrNode

Returns a new instance of OrNode.



3
4
5
6
# File 'lib/datacaster/or_node.rb', line 3

def initialize(left, right)
  @left = left
  @right = right
end

Instance Method Details

#cast(object, runtime:) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/datacaster/or_node.rb', line 8

def cast(object, runtime:)
  left_result = @left.with_runtime(runtime).(object)

  return left_result if left_result.valid?

  @right.with_runtime(runtime).(object)
end

#inspectObject



30
31
32
# File 'lib/datacaster/or_node.rb', line 30

def inspect
  "#<Datacaster::OrNode L: #{@left.inspect} R: #{@right.inspect}>"
end

#to_json_schemaObject



16
17
18
19
20
# File 'lib/datacaster/or_node.rb', line 16

def to_json_schema
  JsonSchemaResult.new({
    "anyOf" => [@left, @right].map(&:to_json_schema)
  })
end

#to_json_schema_attributesObject



22
23
24
25
26
27
28
# File 'lib/datacaster/or_node.rb', line 22

def to_json_schema_attributes
  super.merge(
    required:
      @left.to_json_schema_attributes[:required] &&
        @right.to_json_schema_attributes[:required]
  )
end