Class: Akasha::Repository
- Inherits:
-
Object
- Object
- Akasha::Repository
- Defined in:
- lib/akasha/repository.rb
Overview
Aggregate repository. Not meant to be used directly (see aggregate/syntax_helpers.rb) See specs for usage.
Constant Summary collapse
- STREAM_NAME_SEP =
'-'.freeze
Instance Method Summary collapse
-
#initialize(store) ⇒ Repository
constructor
Creates a new repository using the underlying ‘store` (e.g. `MemoryEventStore`).
-
#load_aggregate(klass, id) ⇒ Object
Loads an aggregate identified by ‘id` and `klass` from the repository.
-
#save_aggregate(aggregate) ⇒ Object
Saves an aggregate to the repository, appending events to the corresponding stream.
-
#subscribe(lambda = nil, &block) ⇒ Object
Subscribes to event streams passing either a lambda or a block.
Constructor Details
#initialize(store) ⇒ Repository
Creates a new repository using the underlying ‘store` (e.g. `MemoryEventStore`).
9 10 11 12 |
# File 'lib/akasha/repository.rb', line 9 def initialize(store) @store = store @subscribers = [] end |
Instance Method Details
#load_aggregate(klass, id) ⇒ Object
Loads an aggregate identified by ‘id` and `klass` from the repository. Returns an aggregate instance of class `klass` constructed by applying events from the corresponding stream.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/akasha/repository.rb', line 17 def load_aggregate(klass, id) agg = klass.new(id) start = 0 page_size = 20 stream(klass, id).read_events(start, page_size) do |events| agg.apply_events(events) end agg end |
#save_aggregate(aggregate) ⇒ Object
Saves an aggregate to the repository, appending events to the corresponding stream.
30 31 32 33 34 |
# File 'lib/akasha/repository.rb', line 30 def save_aggregate(aggregate) changeset = aggregate.changeset stream(aggregate.class, changeset.aggregate_id).write_events(changeset.events) notify_subscribers(aggregate) end |
#subscribe(lambda = nil, &block) ⇒ Object
Subscribes to event streams passing either a lambda or a block. Example:
repo.subscribe do |aggregate_id, event|
... handle the event ...
end
42 43 44 45 |
# File 'lib/akasha/repository.rb', line 42 def subscribe(lambda = nil, &block) callable = lambda || block @subscribers << callable end |