Sha256: 22ae5db3d98f052c240a99df4f7f94ff32785bd82070b0091df8f66e03f8640e

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 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

          validate_state!

          @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 start(input)
          super
          input      = input_path.value(context, input)
          next_state = choices.detect { |choice| choice.true?(context, input) }&.next || default
          output     = output_path.value(context, input)

          context.next_state = next_state
          context.output     = output
        end

        def running?
          false
        end

        def end?
          false
        end

        private

        def validate_state!
          validate_state_choices!
          validate_state_default!
        end

        def validate_state_choices!
          raise Floe::InvalidWorkflowError, "Choice state must have \"Choices\"" unless payload.key?("Choices")
          raise Floe::InvalidWorkflowError, "\"Choices\" must be a non-empty array" unless payload["Choices"].kind_of?(Array) && !payload["Choices"].empty?
        end

        def validate_state_default!
          raise Floe::InvalidWorkflowError, "\"Default\" not in \"States\"" unless workflow.payload["States"].include?(payload["Default"])
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
floe-0.9.0 lib/floe/workflow/states/choice.rb
floe-0.7.1 lib/floe/workflow/states/choice.rb
floe-0.8.0 lib/floe/workflow/states/choice.rb
floe-0.7.0 lib/floe/workflow/states/choice.rb
floe-0.6.1 lib/floe/workflow/states/choice.rb
floe-0.6.0 lib/floe/workflow/states/choice.rb