Class: Trailblazer::Activity::Circuit::TaskAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/trailblazer/activity/circuit/task_adapter.rb

Direct Known Subclasses

TaskWrap::Pipeline::TaskAdapter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(circuit_step) ⇒ TaskAdapter

Returns a new instance of TaskAdapter.

Parameters:

  • Exposes (circuit_step)

    a Circuit::Step.call([ctx, flow_options], **circuit_options) interface



68
69
70
# File 'lib/trailblazer/activity/circuit/task_adapter.rb', line 68

def initialize(circuit_step, **)
  @circuit_step = circuit_step
end

Class Method Details

.binary_signal_for(result, on_true, on_false) ⇒ Object

Translates the return value of the user step into a valid signal. Note that it passes through subclasses of Signal.



83
84
85
86
87
88
89
# File 'lib/trailblazer/activity/circuit/task_adapter.rb', line 83

def self.binary_signal_for(result, on_true, on_false)
  if result.is_a?(Class) && result < Activity::Signal
    result
  else
    result ? on_true : on_false
  end
end

.for_step(step, binary: true, **options_for_step) ⇒ TaskAdapter

Note:

This used to be called ‘TaskBuilder::Task`.

Returns a ‘TaskAdapter` instance always exposes the complete circuit-interface, and can be used directly in a Trailblazer::Activity::Circuit.

Parameters:

  • Any (step)

    kind of callable object or ‘:instance_method` that receives a step interface.

  • If (:option)

    true, the user’s callable argument is wrapped in ‘Trailblazer::Option`.

Returns:

  • (TaskAdapter)

    a circuit-interface compatible object to use in a ‘Circuit`.



61
62
63
64
65
# File 'lib/trailblazer/activity/circuit/task_adapter.rb', line 61

def self.for_step(step, binary: true, **options_for_step)
  circuit_step = Circuit.Step(step, **options_for_step)

  new(circuit_step)
end

Instance Method Details

#call(ctx, flow_options, **circuit_options) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/trailblazer/activity/circuit/task_adapter.rb', line 72

def call((ctx, flow_options), **circuit_options)
  result, ctx = @circuit_step.([ctx, flow_options], **circuit_options)

  # Return an appropriate signal which direction to go next.
  signal = TaskAdapter.binary_signal_for(result, Activity::Right, Activity::Left)

  return signal, [ctx, flow_options]
end

#inspectObject Also known as: to_s

TODO: make me private!



91
92
93
94
95
# File 'lib/trailblazer/activity/circuit/task_adapter.rb', line 91

def inspect # TODO: make me private!
  user_step = @circuit_step.instance_variable_get(:@user_proc) # DISCUSS: to we want Step#to_h?

  %(#<Trailblazer::Activity::TaskBuilder::Task user_proc=#{Trailblazer::Activity::Introspect.render_task(user_step)}>)
end