Skip to content

Commit

Permalink
Merge pull request #6 from jandudulski/decider-pattern-matching
Browse files Browse the repository at this point in the history
Utilize pattern matching in decider pattern
  • Loading branch information
mostlyobvious authored Nov 20, 2024
2 parents d15e9c1 + 875adcb commit 4f542c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 65 deletions.
4 changes: 2 additions & 2 deletions examples/decider/lib/project_management/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def call(cmd)
state = @repository.load(cmd.id, @decider)

case @decider.decide(cmd, state)
in StandardError
in [StandardError]
raise Error
in Event => event
in [Event => event]
@repository.store(cmd.id, event)
end
end
Expand Down
78 changes: 15 additions & 63 deletions examples/decider/lib/project_management/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ module Issue

class << self
def decide(command, state)
case command
when CreateIssue
open(state)
when ResolveIssue
resolve(state)
when CloseIssue
close(state)
when ReopenIssue
reopen(state)
when StartIssueProgress
start(state)
when StopIssueProgress
stop(state)
case [command, state]
in [CreateIssue, State(id:, status: nil)]
[IssueOpened.new(data: { issue_id: id })]
in [ResolveIssue, State(id:, status: :open | :in_progress | :reopened)]
[IssueResolved.new(data: { issue_id: id })]
in [CloseIssue, State(id:, status: :open | :in_progress | :resolved | :reopened)]
[IssueClosed.new(data: { issue_id: id })]
in [ReopenIssue, State(id:, status: :resolved | :closed)]
[IssueReopened.new(data: { issue_id: id })]
in [StartIssueProgress, State(id:, status: :open | :reopened)]
[IssueProgressStarted.new(data: { issue_id: id })]
in [StopIssueProgress, State(id:, status: :in_progress)]
[IssueProgressStopped.new(data: { issue_id: id })]
else
[InvalidTransition.new]
end
end

Expand All @@ -41,56 +43,6 @@ def evolve(state, event)
def initial_state(id)
State.new(id: id, status: nil)
end

private

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

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

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

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

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

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

0 comments on commit 4f542c3

Please sign in to comment.