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 Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
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
idandklassfrom the repository. -
#merge_all_by_event(into:, only:) ⇒ Object
Merges all streams into one, filtering the resulting stream so it only contains events with the specified names, using a projection.
-
#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).
11 12 13 14 |
# File 'lib/akasha/repository.rb', line 11 def initialize(store) @store = store @subscribers = [] end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
6 7 8 |
# File 'lib/akasha/repository.rb', line 6 def store @store 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.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/akasha/repository.rb', line 19 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 |
#merge_all_by_event(into:, only:) ⇒ Object
Merges all streams into one, filtering the resulting stream so it only contains events with the specified names, using a projection.
Arguments:
`into` - name of the new stream
`only` - array of event names
56 57 58 |
# File 'lib/akasha/repository.rb', line 56 def merge_all_by_event(into:, only:) @store.merge_all_by_event(into: into, only: only) end |
#save_aggregate(aggregate) ⇒ Object
Saves an aggregate to the repository, appending events to the corresponding stream.
32 33 34 35 36 |
# File 'lib/akasha/repository.rb', line 32 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
44 45 46 47 |
# File 'lib/akasha/repository.rb', line 44 def subscribe(lambda = nil, &block) callable = lambda || block @subscribers << callable end |