Class: RubyCqrs::Data::InMemoryEventStore

Inherits:
Object
  • Object
show all
Includes:
Contracts, Contracts::Modules, EventStore
Defined in:
lib/ruby_cqrs/data/in_memory_event_store.rb

Instance Method Summary collapse

Constructor Details

#initializeInMemoryEventStore

Returns a new instance of InMemoryEventStore.



12
13
14
15
16
# File 'lib/ruby_cqrs/data/in_memory_event_store.rb', line 12

def initialize
  @aggregate_store = {}
  @event_store = {}
  @snapshot_store = {}
end

Instance Method Details

#load_by(guid, command_context) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_cqrs/data/in_memory_event_store.rb', line 21

def load_by guid, command_context
  key = guid.to_sym
  state = { :aggregate_id => guid,
            :aggregate_type => @aggregate_store[key][:type] }

  if @snapshot_store.has_key? key
    extract_snapshot_into key, state
  else
    state[:events] = @event_store[key][:events]
  end

  state
end

#save(changes, command_context) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_cqrs/data/in_memory_event_store.rb', line 37

def save changes, command_context
  changes.each do |change|
    key = change[:aggregate_id].to_sym
    verify_state key, change
  end
  changes.each do |change|
    key = change[:aggregate_id].to_sym
    create_state key, change
    update_state key, change
  end
  nil
end