Class: Aggregates::EventStream

Inherits:
Object
  • Object
show all
Defined in:
lib/aggregates/event_stream.rb

Overview

An EventStream is a sequence, append only sequence of events that are read when reconstructing aggregates and written to when a command is processed by the aggregate.

There is likely no need to interact with this class directly.

Instance Method Summary collapse

Constructor Details

#initialize(storage_backend, event_processors, aggregate_id) ⇒ EventStream

Returns a new instance of EventStream.



9
10
11
12
13
# File 'lib/aggregates/event_stream.rb', line 9

def initialize(storage_backend, event_processors, aggregate_id)
  @storage_backend = storage_backend
  @event_processors = event_processors
  @aggregate_id = aggregate_id
end

Instance Method Details

#load_events(ending_at: nil) ⇒ Object



15
16
17
18
19
# File 'lib/aggregates/event_stream.rb', line 15

def load_events(ending_at: nil)
  events = @storage_backend.load_events_by_aggregate_id(@aggregate_id)
  events = events.select { |event| event.created_at <= ending_at } if ending_at.present?
  events
end

#publish(event) ⇒ Object



21
22
23
24
# File 'lib/aggregates/event_stream.rb', line 21

def publish(event)
  send_to_event_processors(event)
  store_event(event)
end