Skip to content

Commit

Permalink
Extract repository
Browse files Browse the repository at this point in the history
  • Loading branch information
mostlyobvious committed Nov 27, 2023
1 parent 896411b commit abbbb91
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions examples/functional/lib/project_management.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative "../../../shared/lib/project_management"
require_relative "project_management/handler"
require_relative "project_management/issue"
require_relative "project_management/repository"
require_relative "project_management/issue_state"
14 changes: 5 additions & 9 deletions examples/functional/lib/project_management/handler.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module ProjectManagement
class Handler
def initialize(event_store) = @event_store = event_store
def initialize(event_store)
@repository = Repository.new(event_store)
end

def call(cmd)
case cmd
Expand Down Expand Up @@ -28,20 +30,14 @@ def stop(id) = with_state(id) { |state| Issue.stop(state) }

private

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

def with_state(id)
state =
@event_store
.read
.stream(stream_name(id))
.reduce(IssueState.initial(id)) { |state, event| state.apply(event) }
state = @repository.load(id, IssueState.initial(id))

case yield(state)
in StandardError
raise Error
in Event => event
@event_store.append(event, stream_name: stream_name(id))
@repository.store(id, event)
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions examples/functional/lib/project_management/repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module ProjectManagement
class Repository
def initialize(event_store)
@event_store = event_store
end

def load(id, initial_state)
@event_store
.read
.stream(stream_name(id))
.reduce(initial_state) { |state, event| state.apply(event) }
end

def store(id, events)
@event_store.append(events, stream_name: stream_name(id))
end

private

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

0 comments on commit abbbb91

Please sign in to comment.