-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Szymon Fiedler <[email protected]> Co-authored-by: Piotr Jurewicz <[email protected]>
- Loading branch information
1 parent
568665f
commit 377b24a
Showing
2 changed files
with
50 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,74 @@ | ||
module ProjectManagement | ||
module Issue | ||
class Issue | ||
InvalidTransition = Class.new(StandardError) | ||
State = Data.define(:id, :status) | ||
private_constant :State | ||
|
||
def self.decide(command, state) | ||
def initialize(id) | ||
@state = initial_state(id) | ||
end | ||
|
||
def decide(command) | ||
case command | ||
when CreateIssue | ||
if state.status | ||
if @state.status | ||
InvalidTransition.new | ||
else | ||
IssueOpened.new(data: { issue_id: state.id }) | ||
IssueOpened.new(data: { issue_id: @state.id }) | ||
end | ||
when ResolveIssue | ||
if %i[open in_progress reopened].include? state.status | ||
IssueResolved.new(data: { issue_id: state.id }) | ||
if %i[open in_progress reopened].include? @state.status | ||
IssueResolved.new(data: { issue_id: @state.id }) | ||
else | ||
InvalidTransition.new | ||
end | ||
when CloseIssue | ||
if %i[open in_progress resolved reopened].include? state.status | ||
IssueClosed.new(data: { issue_id: state.id }) | ||
if %i[open in_progress resolved reopened].include? @state.status | ||
IssueClosed.new(data: { issue_id: @state.id }) | ||
else | ||
InvalidTransition.new | ||
end | ||
when ReopenIssue | ||
if %i[resolved closed].include? state.status | ||
IssueReopened.new(data: { issue_id: state.id }) | ||
if %i[resolved closed].include? @state.status | ||
IssueReopened.new(data: { issue_id: @state.id }) | ||
else | ||
InvalidTransition.new | ||
end | ||
when StartIssueProgress | ||
if %i[open reopened].include? state.status | ||
IssueProgressStarted.new(data: { issue_id: state.id }) | ||
if %i[open reopened].include? @state.status | ||
IssueProgressStarted.new(data: { issue_id: @state.id }) | ||
else | ||
InvalidTransition.new | ||
end | ||
when StopIssueProgress | ||
if %i[in_progress].include? state.status | ||
IssueProgressStopped.new(data: { issue_id: state.id }) | ||
if %i[in_progress].include? @state.status | ||
IssueProgressStopped.new(data: { issue_id: @state.id }) | ||
else | ||
InvalidTransition.new | ||
end | ||
end | ||
end | ||
|
||
def self.evolve(state, event) | ||
case event | ||
when IssueOpened | ||
state.with(status: :open) | ||
when IssueResolved | ||
state.with(status: :resolved) | ||
when IssueClosed | ||
state.with(status: :closed) | ||
when IssueReopened | ||
state.with(status: :reopened) | ||
when IssueProgressStarted | ||
state.with(status: :in_progress) | ||
when IssueProgressStopped | ||
state.with(status: :open) | ||
end | ||
def evolve(event) | ||
@state = | ||
case event | ||
when IssueOpened | ||
@state.with(status: :open) | ||
when IssueResolved | ||
@state.with(status: :resolved) | ||
when IssueClosed | ||
@state.with(status: :closed) | ||
when IssueReopened | ||
@state.with(status: :reopened) | ||
when IssueProgressStarted | ||
@state.with(status: :in_progress) | ||
when IssueProgressStopped | ||
@state.with(status: :open) | ||
end | ||
end | ||
|
||
def self.initial_state(id) | ||
State.new(id: id, status: nil) | ||
end | ||
private | ||
|
||
def initial_state(id) = State.new(id: id, status: nil) | ||
end | ||
end |