Class: EventSourcery::Repository
- Inherits:
-
Object
- Object
- EventSourcery::Repository
- Defined in:
- lib/event_sourcery/repository.rb
Overview
This class provides a set of methods to help load and save aggregate instances.
Refer to EventSourceryTodoApp for a more complete example.
Class Method Summary collapse
-
.load(aggregate_class, aggregate_id, event_source:, event_sink:) ⇒ Object
Create a new instance of the repository and load an aggregate instance.
Instance Method Summary collapse
-
#initialize(event_source:, event_sink:) ⇒ Repository
constructor
A new instance of Repository.
-
#load(aggregate_class, aggregate_id) ⇒ Object
Load an aggregate instance.
-
#save(aggregate) ⇒ Object
Save any new events/changes in the provided aggregate to the event sink.
Constructor Details
#initialize(event_source:, event_sink:) ⇒ Repository
Returns a new instance of Repository.
29 30 31 32 |
# File 'lib/event_sourcery/repository.rb', line 29 def initialize(event_source:, event_sink:) @event_source = event_source @event_sink = event_sink end |
Class Method Details
.load(aggregate_class, aggregate_id, event_source:, event_sink:) ⇒ Object
Create a new instance of the repository and load an aggregate instance
22 23 24 25 |
# File 'lib/event_sourcery/repository.rb', line 22 def self.load(aggregate_class, aggregate_id, event_source:, event_sink:) new(event_source: event_source, event_sink: event_sink) .load(aggregate_class, aggregate_id) end |
Instance Method Details
#load(aggregate_class, aggregate_id) ⇒ Object
Load an aggregate instance
38 39 40 41 |
# File 'lib/event_sourcery/repository.rb', line 38 def load(aggregate_class, aggregate_id) events = event_source.get_events_for_aggregate_id(aggregate_id) aggregate_class.new(aggregate_id, events) end |
#save(aggregate) ⇒ Object
Save any new events/changes in the provided aggregate to the event sink
46 47 48 49 50 51 52 53 |
# File 'lib/event_sourcery/repository.rb', line 46 def save(aggregate) new_events = aggregate.changes if new_events.any? event_sink.sink(new_events, expected_version: aggregate.version - new_events.count) end aggregate.clear_changes end |