Class: Akasha::Repository

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(store, namespace: nil) ⇒ Repository

Creates a new repository using the underlying `store` (e.g. `MemoryEventStore`).


11
12
13
14
15
# File 'lib/akasha/repository.rb', line 11

def initialize(store, namespace: nil)
  @store = store
  @subscribers = []
  @namespace = namespace
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace


6
7
8
# File 'lib/akasha/repository.rb', line 6

def namespace
  @namespace
end

#storeObject (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.


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/akasha/repository.rb', line 20

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

58
59
60
# File 'lib/akasha/repository.rb', line 58

def merge_all_by_event(into:, only:)
  @store.merge_all_by_event(into: into, only: only, namespace: @namespace)
end

#save_aggregate(aggregate) ⇒ Object

Saves an aggregate to the repository, appending events to the corresponding stream.


33
34
35
36
37
38
# File 'lib/akasha/repository.rb', line 33

def save_aggregate(aggregate)
  changeset = aggregate.changeset
  events = changeset.events.map { |event| event.(namespace: @namespace) }
  stream(aggregate.class, changeset.aggregate_id).write_events(events)
  notify_subscribers(changeset.aggregate_id, events)
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

46
47
48
49
# File 'lib/akasha/repository.rb', line 46

def subscribe(lambda = nil, &block)
  callable = lambda || block
  @subscribers << callable
end