class Trailblazer::Macro::Nested::Dynamic

For dynamic `Nested`s that do not expose an {Activity} interface.

Dynamic doesn't automatically connect outputs of runtime {Activity} at compile time (as we don't know which activity will be nested, obviously). So by default, it only connects good old success/failure ends. But it is also possible to connect all the ends of all possible dynamic activities by passing their list to {:auto_wire} option.

step Nested(:compute_nested, auto_wire: [Create, Update])

Constants

STATIC_OUTPUTS

Attributes

outputs[R]

Public Class Methods

new(nested_activity_decider, auto_wire: []) click to toggle source
# File lib/trailblazer/macro/nested.rb, line 50
def initialize(nested_activity_decider, auto_wire: [])
  @nested_activity_decider  = Trailblazer::Option(nested_activity_decider)
  @known_activities         = Array(auto_wire)
  @outputs                  = compute_task_outputs
end

Public Instance Methods

compute_nested_activity(wrap_ctx, original_args) click to toggle source

TaskWrap step.

# File lib/trailblazer/macro/nested.rb, line 59
def compute_nested_activity(wrap_ctx, original_args)
  (ctx, _), original_circuit_options = original_args

  # TODO: evaluate the option to get the actual "object" to call.
  activity = @nested_activity_decider.(ctx, keyword_arguments: ctx.to_hash, **original_circuit_options)

  # Overwrite :task so task_wrap.call_task will call this activity.
  # This is a trick so we don't have to repeat logic from #call_task here.
  wrap_ctx[:task] = activity

  return wrap_ctx, original_args
end
compute_return_signal(wrap_ctx, original_args) click to toggle source
# File lib/trailblazer/macro/nested.rb, line 72
def compute_return_signal(wrap_ctx, original_args)
  # NOOP when @known_activities are present as all possible signals have been registered already.
  if @known_activities.empty?
    # Translate the genuine nested signal to the generic Dynamic end (success/failure, only).
    # Note that here we lose information about what specific event was emitted.
    wrap_ctx[:return_signal] = wrap_ctx[:return_signal].kind_of?(Activity::Railway::End::Success) ?
      @outputs[:success].signal : @outputs[:failure].signal
  end

  return wrap_ctx, original_args
end

Private Instance Methods

compute_task_outputs() click to toggle source
# File lib/trailblazer/macro/nested.rb, line 84
        def compute_task_outputs
  # If :auto_wire is empty, we map outputs to :success and :failure only, for backward compatibility.
  # This is what {Nested} in 2.0 used to do, where the outcome could only be true/false (or success/failure).
  return STATIC_OUTPUTS if @known_activities.empty?

  # Merge activity#outputs from all given auto_wirable activities to wire up for this dynamic task.
  @known_activities.map do |activity|
    # TODO: Replace this when it's helper gets added.
    Hash[activity.to_h[:outputs].collect{ |output| [output.semantic, output] }]
  end.inject(:merge)
end