Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
mostlyobvious committed Nov 20, 2023
1 parent 2fd896d commit 7a183be
Showing 2 changed files with 43 additions and 75 deletions.
74 changes: 29 additions & 45 deletions examples/duck_typing/lib/project_management/command_handler.rb
Original file line number Diff line number Diff line change
@@ -9,94 +9,78 @@ def create(cmd)
issue.open
IssueOpened.new(data: { issue_id: cmd.id })
end
rescue NoMethodError
raise_invalid
end

def close(cmd)
load_issue(cmd.id) do |issue|
issue.close
IssueClosed.new(data: { issue_id: cmd.id })
end
rescue NoMethodError
raise_invalid
end

def start(cmd)
load_issue(cmd.id) do |issue|
issue.start
IssueProgressStarted.new(data: { issue_id: cmd.id })
end
rescue NoMethodError
raise_invalid
end

def stop(cmd)
load_issue(cmd.id) do |issue|
issue.stop
IssueProgressStopped.new(data: { issue_id: cmd.id })
end
rescue NoMethodError
raise_invalid
end

def reopen(cmd)
load_issue(cmd.id) do |issue|
issue.reopen
IssueReopened.new(data: { issue_id: cmd.id })
end
rescue NoMethodError
raise_invalid
end

def resolve(cmd)
load_issue(cmd.id) do |issue|
issue.resolve
IssueResolved.new(data: { issue_id: cmd.id })
end
rescue NoMethodError
raise_invalid
end

private

def raise_invalid
raise Error
end

def stream_name(id)
"Issue$#{id}"
end
def stream_name(id) = "Issue$#{id}"

def load_issue(id)
version = -1
issue = Issue.new
@event_store
.read
.stream(stream_name(id))
.each do |event|
case event
when IssueOpened
issue = issue.open
when IssueProgressStarted
issue = issue.start
when IssueProgressStopped
issue = issue.stop
when IssueResolved
issue = issue.resolve
when IssueReopened
issue = issue.reopen
when IssueClosed
issue = issue.close
issue, version =
@event_store
.read
.stream(stream_name(id))
.reduce([Issue.new, -1]) do |(issue, version), event|
new_issue =
case event
when IssueOpened
issue.open
when IssueProgressStarted
issue.start
when IssueProgressStopped
issue.stop
when IssueResolved
issue.resolve
when IssueReopened
issue.reopen
when IssueClosed
issue.close
end
[new_issue, version + 1]
end
version += 1
end
events = yield issue
publish(events, id, version)
end

def publish(events, id, version)
@event_store.publish(events, stream_name: stream_name(id), expected_version: version)
@event_store.publish(
yield(issue),
stream_name: stream_name(id),
expected_version: version
)
rescue NoMethodError
raise Error
end
end
end
44 changes: 14 additions & 30 deletions examples/duck_typing/lib/project_management/issue.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,26 @@
module ProjectManagement
class Issue
def open
Open.new
end
def open = Open.new
end

class Open
def start
InProgress.new
end
def resolve
Resolved.new
end
def close
Closed.new
end
def start = InProgress.new
def resolve = Resolved.new
def close = Closed.new
end

class InProgress
def stop
Open.new
end
def close
Closed.new
end
def resolve
Resolved.new
end
def stop = Open.new
def close = Closed.new
def resolve = Resolved.new
end

class Resolved
def close
Closed.new
end
def reopen
Open.new
end
def close = Closed.new
def reopen = Open.new
end

class Closed
def reopen
Open.new
end
def reopen = Open.new
end
end

0 comments on commit 7a183be

Please sign in to comment.