Class: EventSourcing::Event::Store::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/event_sourcing/event/store/memory.rb

Instance Method Summary collapse

Constructor Details

#initializeMemory

Returns a new instance of Memory.



9
10
11
# File 'lib/event_sourcing/event/store/memory.rb', line 9

def initialize
  @events_with_stream_id = []
end

Instance Method Details

#append(stream_id, expected_version, events) ⇒ Object Also known as: <<, push



22
23
24
25
26
27
28
29
30
31
# File 'lib/event_sourcing/event/store/memory.rb', line 22

def append(stream_id, expected_version, events)
  raise EventSourcing::Event::Store::ConcurrencyError if get_stream(stream_id).version != expected_version

  Array(events).tap do |events|
    events.each do |event|
      @events_with_stream_id.push(stream_id: stream_id, event: event)
    end
  end
  nil
end

#eventsObject



13
14
15
# File 'lib/event_sourcing/event/store/memory.rb', line 13

def events
  @events_with_stream_id.map { |e| e[:event] }
end

#get_stream(id) ⇒ Object



17
18
19
20
# File 'lib/event_sourcing/event/store/memory.rb', line 17

def get_stream(id)
  events = events_for(id)
  EventSourcing::Event::Stream.new(id, events, events.count, self)
end