Class: TypedOperation::SmartChain

Inherits:
ChainedOperation show all
Includes:
CallableResolver, Result::Mixin
Defined in:
lib/typed_operation/chains/smart_chain.rb

Overview

Smart chain created by .then Accumulates context across chain steps and extracts required params for each operation.

Instance Method Summary collapse

Methods included from CallableResolver

#extract_operation_class

Methods included from Operations::Composition

#or_else, #then, #then_passes, #then_spreads, #transform

Constructor Details

#initialize(left, right, extra_kwargs = {}) ⇒ SmartChain

: (_Callable, _Callable, ?Hash[Symbol, untyped]) -> void



17
18
19
20
21
22
23
# File 'lib/typed_operation/chains/smart_chain.rb', line 17

def initialize(left, right, extra_kwargs = {})
  super(left, right)
  @extra_kwargs = extra_kwargs
  @uses_kwargs = uses_kwargs?(right)
  @required_params = extract_required_params(right)
  @prefilled_params = extract_prefilled_params(right)
end

Instance Method Details

#call(*args, **kwargs) ⇒ Object

: (*untyped, **untyped) -> _Result[untyped, untyped]



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/typed_operation/chains/smart_chain.rb', line 26

def call(*args, **kwargs)
  # Execute left side
  result = @left.call(*args, **kwargs)
  return result if result.failure?

  # Build context from left's result
  left_value = result.value!
  context = to_context(left_value)

  # Call right side with appropriate params
  right_result = call_right(context)
  return right_result if right_result.failure?

  # Merge right's output into context
  right_value = right_result.value!
  merged = merge_into_context(context, right_value)

  Success(merged)
end