Sha256: 62cd350e3051bf56264f1a11e00856226022d811a812edc18eee50feff81f803

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module Trailblazer::V2_1
  module Operation::Railway
    # every step is wrapped by this proc/decider. this is executed in the circuit as the actual task.
    # Step calls step.(options, **options, flow_options)
    # Output direction binary: true=>Right, false=>Left.
    # Passes through all subclasses of Direction.~~~~~~~~~~~~~~~~~
    module TaskBuilder
      def self.call(user_proc)
        Task.new( Trailblazer::V2_1::Option::KW( user_proc ), user_proc )
      end

      # Translates the return value of the user step into a valid signal.
      # Note that it passes through subclasses of {Signal}.
      def self.binary_direction_for(result, on_true, on_false)
        result.is_a?(Class) && result < Activity::Signal ? result : (result ? on_true : on_false)
      end
    end

    class Task
      def initialize(task, user_proc)
        @task      = task
        @user_proc = user_proc
        freeze
      end

      def call( (options, *args), **circuit_args )
        # Execute the user step with TRB's kw args.
        result = @task.( options, **circuit_args ) # circuit_args contains :exec_context.

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

        [ direction, [ options, *args ], **circuit_args ]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trailblazer-future-2.1.0.rc1 lib/trailblazer/v2_1/operation/railway/task_builder.rb