Class: Aggregates::InMemoryStorageBackend

Inherits:
StorageBackend show all
Defined in:
lib/aggregates/in_memory_storage_backend.rb

Overview

This is an extremely simple storage backend that retains all events and commands in process memory. This method does not persist beyond application restarts and should generally only be used in testing.

Instance Method Summary collapse

Constructor Details

#initializeInMemoryStorageBackend

Returns a new instance of InMemoryStorageBackend.



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

def initialize
  super()

  @events = {}
  @commands = {}
end

Instance Method Details

#load_commands_by_aggregate_id(aggregate_id) ⇒ Object



29
30
31
# File 'lib/aggregates/in_memory_storage_backend.rb', line 29

def load_commands_by_aggregate_id(aggregate_id)
  @commands[aggregate_id] ||= []
end

#load_events_by_aggregate_id(aggregate_id) ⇒ Object



25
26
27
# File 'lib/aggregates/in_memory_storage_backend.rb', line 25

def load_events_by_aggregate_id(aggregate_id)
  @events[aggregate_id] ||= []
end

#store_command(command) ⇒ Object



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

def store_command(command)
  commands_for_aggregate_id = load_commands_by_aggregate_id(command.aggregate_id)
  commands_for_aggregate_id << command
end

#store_event(event) ⇒ Object



20
21
22
23
# File 'lib/aggregates/in_memory_storage_backend.rb', line 20

def store_event(event)
  event_for_aggregate_id = load_events_by_aggregate_id(event.aggregate_id)
  event_for_aggregate_id << event
end