Skip to content

Commit

Permalink
Extract state transitions
Browse files Browse the repository at this point in the history
Co-authored-by: Szymon Fiedler <[email protected]>
Co-authored-by: Piotr Jurewicz <[email protected]>
  • Loading branch information
3 people committed Nov 22, 2023
1 parent 377b24a commit 1d09456
Showing 1 changed file with 54 additions and 30 deletions.
84 changes: 54 additions & 30 deletions examples/decider/lib/project_management/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,17 @@ def initialize(id)
def decide(command)
case command
when CreateIssue
if @state.status
InvalidTransition.new
else
IssueOpened.new(data: { issue_id: @state.id })
end
open
when ResolveIssue
if %i[open in_progress reopened].include? @state.status
IssueResolved.new(data: { issue_id: @state.id })
else
InvalidTransition.new
end
resolve
when CloseIssue
if %i[open in_progress resolved reopened].include? @state.status
IssueClosed.new(data: { issue_id: @state.id })
else
InvalidTransition.new
end
close
when ReopenIssue
if %i[resolved closed].include? @state.status
IssueReopened.new(data: { issue_id: @state.id })
else
InvalidTransition.new
end
reopen
when StartIssueProgress
if %i[open reopened].include? @state.status
IssueProgressStarted.new(data: { issue_id: @state.id })
else
InvalidTransition.new
end
start
when StopIssueProgress
if %i[in_progress].include? @state.status
IssueProgressStopped.new(data: { issue_id: @state.id })
else
InvalidTransition.new
end
stop
end
end

Expand All @@ -69,6 +45,54 @@ def evolve(event)

private

def open
if @state.status
InvalidTransition.new
else
IssueOpened.new(data: { issue_id: @state.id })
end
end

def resolve
if %i[open in_progress reopened].include? @state.status
IssueResolved.new(data: { issue_id: @state.id })
else
InvalidTransition.new
end
end

def close
if %i[open in_progress resolved reopened].include? @state.status
IssueClosed.new(data: { issue_id: @state.id })
else
InvalidTransition.new
end
end

def reopen
if %i[resolved closed].include? @state.status
IssueReopened.new(data: { issue_id: @state.id })
else
InvalidTransition.new
end
end

def stop
if %i[in_progress].include? @state.status
IssueProgressStopped.new(data: { issue_id: @state.id })
else
InvalidTransition.new
end
end

def start
if %i[open reopened].include? @state.status
IssueProgressStarted.new(data: { issue_id: @state.id })
else
InvalidTransition.new
end
end

def initial_state(id) = State.new(id: id, status: nil)
end
end

0 comments on commit 1d09456

Please sign in to comment.