Skip to content

Commit

Permalink
Simplify next state handling only return state name
Browse files Browse the repository at this point in the history
The State classes don't need to return the next state object, just the
name.  The Workflow class can lookup the state object by the name since
it owns the hash of state objects.
  • Loading branch information
agrare committed Jul 20, 2023
1 parent 90a4e3f commit f5937e1
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
8 changes: 3 additions & 5 deletions lib/floe/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ def step

context.states << context.state

@status = current_state.status
@output = output if end?

next_state_name = next_state&.name
@current_state = next_state_name && @states_by_name[next_state_name]
@status = current_state.status
@current_state = next_state && @states_by_name[next_state]
@output = output if end?

self
end
Expand Down
4 changes: 2 additions & 2 deletions lib/floe/workflow/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def run!(input)
input = input_path.value(context, input)

output, next_state = block_given? ? yield(input) : input
next_state ||= workflow.states_by_name[payload["Next"]] unless end?
next_state ||= payload["Next"] unless end?

output ||= input
output = output_path&.value(context, output)

logger.info("Running state: [#{name}] with input [#{input}]...Complete - next state: [#{next_state&.name}] output: [#{output}]")
logger.info("Running state: [#{name}] with input [#{input}]...Complete - next state: [#{next_state}] output: [#{output}]")

[next_state, output]
end
Expand Down
6 changes: 2 additions & 4 deletions lib/floe/workflow/states/choice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ def initialize(workflow, name, payload)

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 = input
next_state = choices.detect { |choice| choice.true?(context, input) }&.next || default

[output, next_state]
end
Expand Down
4 changes: 2 additions & 2 deletions spec/workflow/states/choice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

it "returns the next state" do
next_state, = subject
expect(next_state).to eq(workflow.states_by_name["FirstMatchState"])
expect(next_state).to eq("FirstMatchState")
end
end

Expand All @@ -26,7 +26,7 @@

it "returns the default state" do
next_state, = subject
expect(next_state).to eq(workflow.states_by_name["FailState"])
expect(next_state).to eq("FailState")
end
end
end
Expand Down

0 comments on commit f5937e1

Please sign in to comment.