Class: Lieutenant::EventStore::InMemory

Inherits:
Object
  • Object
show all
Defined in:
lib/lieutenant/event_store/in_memory.rb

Overview

Memory implementation of the event store. Stores events while the application is running

Instance Method Summary collapse

Constructor Details

#initializeInMemory

Returns a new instance of InMemory.



7
8
9
10
# File 'lib/lieutenant/event_store/in_memory.rb', line 7

def initialize
  @store = []
  @index = {}
end

Instance Method Details

#aggregate_sequence_number(aggregate_id) ⇒ Object



26
27
28
29
# File 'lib/lieutenant/event_store/in_memory.rb', line 26

def aggregate_sequence_number(aggregate_id)
  return -1 unless index.key?(aggregate_id)
  store[index[aggregate_id].last].sequence_number
end

#event_stream_for(aggregate_id) ⇒ Object



19
20
21
22
23
24
# File 'lib/lieutenant/event_store/in_memory.rb', line 19

def event_stream_for(aggregate_id)
  aggregate_stream = index[aggregate_id]
  return nil unless aggregate_stream
  events = aggregate_stream.lazy.map(&store.method(:[]))
  Enumerator.new { |yielder| events.each(&yielder.method(:<<)) }
end

#persist(events) ⇒ Object



12
13
14
15
16
17
# File 'lib/lieutenant/event_store/in_memory.rb', line 12

def persist(events)
  events.each do |event|
    (index[event.aggregate_id] ||= []).push(store.size)
    store.push(event)
  end
end

#transactionObject



31
32
33
34
# File 'lib/lieutenant/event_store/in_memory.rb', line 31

def transaction
  # In memory event store currently does not support transactions.
  yield
end