Skip to content

Commit

Permalink
Communicate with exceptions
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 1d09456 commit fb96b92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 42 deletions.
17 changes: 7 additions & 10 deletions examples/decider/lib/project_management/command_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ def handle(cmd)
version = idx
end

case result = decider.decide(cmd)
when StandardError
raise Error
else
@event_store.publish(
result,
stream_name: stream_name(cmd.id),
expected_version: version
)
end
@event_store.publish(
decider.decide(cmd),
stream_name: stream_name(cmd.id),
expected_version: version
)
rescue Issue::InvalidTransition
raise Error
end

private
Expand Down
49 changes: 17 additions & 32 deletions examples/decider/lib/project_management/issue.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ProjectManagement
class Issue
InvalidTransition = Class.new(StandardError)

State = Data.define(:id, :status)
private_constant :State

Expand Down Expand Up @@ -46,53 +47,37 @@ def evolve(event)
private

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

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

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

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

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

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

def fail = raise InvalidTransition

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

0 comments on commit fb96b92

Please sign in to comment.