Class: Datacaster::AndNode

Inherits:
Base
  • Object
show all
Defined in:
lib/datacaster/and_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(*casters) ⇒ AndNode



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

def initialize(*casters)
  @casters = casters
end

Instance Method Details

#cast(object, runtime:) ⇒ Object



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

def cast(object, runtime:)
  Datacaster.ValidResult(
    @casters.reduce(object) do |result, caster|
      caster_result = caster.with_runtime(runtime).(result)
      return caster_result unless caster_result.valid?
      caster_result.value
    end
  )
end

#inspectObject



44
45
46
# File 'lib/datacaster/and_node.rb', line 44

def inspect
  "#<Datacaster::AndNode casters: #{@casters.inspect}>"
end

#to_json_schemaObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/datacaster/and_node.rb', line 17

def to_json_schema
  result =
    @casters.reduce(JsonSchemaResult.new) do |result, caster|
      result.apply(caster.to_json_schema, caster.to_json_schema_attributes)
    end

  mapping =
    @casters.reduce({}) do |result, caster|
      result.merge(caster.to_json_schema_attributes[:remaped])
    end

  result.remap(mapping)
end

#to_json_schema_attributesObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/datacaster/and_node.rb', line 31

def to_json_schema_attributes
  super.merge(
    required:
      @casters.any? { |caster| caster.to_json_schema_attributes[:required] },
    picked:
      @casters.flat_map { |caster| caster.to_json_schema_attributes[:picked] },
    remaped:
      @casters.reduce({}) do |result, caster|
        result.merge(caster.to_json_schema_attributes[:remaped])
      end
  )
end