Class: Datacaster::AndWithErrorAggregationNode

Inherits:
Base
  • Object
show all
Defined in:
lib/datacaster/and_with_error_aggregation_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, #then, #with_context, #with_object_context, #with_runtime, #|

Constructor Details

#initialize(left, right) ⇒ AndWithErrorAggregationNode

Returns a new instance of AndWithErrorAggregationNode.



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

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

Instance Method Details

#cast(object, runtime:) ⇒ Object

Works like AndNode, but doesn’t stop at first error — in order to aggregate all Failures Makes sense only for Hash Schemas



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/datacaster/and_with_error_aggregation_node.rb', line 10

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

  if left_result.valid?
    @right.with_runtime(runtime).(left_result.value)
  else
    right_result = @right.with_runtime(runtime).(object)
    if right_result.valid?
      left_result
    else
      Datacaster.ErrorResult(Utils.merge_errors(left_result.raw_errors, right_result.raw_errors))
    end
  end
end

#inspectObject



25
26
27
# File 'lib/datacaster/and_with_error_aggregation_node.rb', line 25

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