Class: RubyCqrs::Data::InMemoryEventStore

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



6
7
8
9
10
# File 'lib/ruby_cqrs/data/in_memory_event_store.rb', line 6

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

Instance Method Details

#load_by(guid, command_context) ⇒ Object

the returned format is as bellow { :aggregate_id => some_aggregtate_id(uuid),

:aggregate_type => the full qualified name of the aggregate type(string),
:events => [ {:aggregate_id => the aggregate_id of the event belongs to(uuid),
              :event_type => the full qualified name of the event type(string),
              :version => the version number of the event(integer),
              :data => protobuf encoded content of the event object(string)}, ..., {} ],
:snapshot => { :state_type => the full qualified name of the snapshot type(string),
               :version => the version number of the aggregate when snapshot(integer),
               :data => protobuf encoded content of the snapshot object(string)} }

the snapshot object could be null; and the events array should return events which has a version number greater than the version number of the returning snapshot, if any.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_cqrs/data/in_memory_event_store.rb', line 24

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

the changes are defined as an array of aggregate change, each change’s format is identical to what above load_by returns



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_cqrs/data/in_memory_event_store.rb', line 40

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