class AggregateRoot::Repository

Attributes

event_store[R]

Public Class Methods

new(event_store = default_event_store) click to toggle source
# File lib/aggregate_root/repository.rb, line 5
def initialize(event_store = default_event_store)
  @event_store = event_store
end

Public Instance Methods

load(aggregate, stream_name) click to toggle source
# File lib/aggregate_root/repository.rb, line 9
def load(aggregate, stream_name)
  event_store.read.stream(stream_name).reduce { |_, ev| aggregate.apply(ev) }
  aggregate.version = aggregate.unpublished_events.count - 1
  aggregate
end
store(aggregate, stream_name) click to toggle source
# File lib/aggregate_root/repository.rb, line 15
def store(aggregate, stream_name)
  event_store.publish(aggregate.unpublished_events.to_a,
    stream_name:      stream_name,
    expected_version: aggregate.version)
  aggregate.version = aggregate.version + aggregate.unpublished_events.count
end
with_aggregate(aggregate, stream_name, &block) click to toggle source
# File lib/aggregate_root/repository.rb, line 22
def with_aggregate(aggregate, stream_name, &block)
  block.call(load(aggregate, stream_name))
  store(aggregate, stream_name)
end

Private Instance Methods

default_event_store() click to toggle source
# File lib/aggregate_root/repository.rb, line 31
def default_event_store
  AggregateRoot.configuration.default_event_store
end