Sha256: 6031d16d6741ea310a3a06422b67862a3dc84e7891c380a34a0ea09351a9c7dc

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Floe
  class Workflow
    module States
      class Choice < Floe::Workflow::State
        attr_reader :choices, :default, :input_path, :output_path

        def initialize(workflow, name, payload)
          super

          @choices = payload["Choices"].map { |choice| ChoiceRule.build(choice) }
          @default = payload["Default"]

          @input_path  = Path.new(payload.fetch("InputPath", "$"))
          @output_path = Path.new(payload.fetch("OutputPath", "$"))
        end

        def run!(*)
          super do |input|
            next_state_name = choices.detect { |choice| choice.true?(context, input) }&.next || default
            next_state      = workflow.states_by_name[next_state_name]

            output = input

            [output, next_state]
          end
        end

        private def to_dot_attributes
          super.merge(:shape => "diamond")
        end

        def to_dot_transitions
          [].tap do |a|
            choices.each do |choice|
              choice_label =
                if choice.payload["NumericEquals"]
                  "#{choice.variable} == #{choice.payload["NumericEquals"]}"
                else
                  "Unknown" # TODO
                end

              a << "  #{name} -> #{choice.next} [ label=#{choice_label.inspect} ]"
            end

            a << "  #{name} -> #{default} [ label=\"Default\" ]" if default
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
floe-0.1.1 lib/floe/workflow/states/choice.rb
floe-0.1.0 lib/floe/workflow/states/choice.rb