Class: Aggregates::Auditor

Inherits:
Object
  • Object
show all
Defined in:
lib/aggregates/auditor.rb

Overview

The Auditor captures the state of a given aggregate at time of use. It provides listings of the commands and events that we executed on a given aggregate.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_backend, type, aggregate_id) ⇒ Auditor

Returns a new instance of Auditor.



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

def initialize(storage_backend, type, aggregate_id)
  @storage_backend = storage_backend
  @type = type
  @aggregate_id = aggregate_id
end

Instance Attribute Details

#aggregate_idObject (readonly)

Returns the value of attribute aggregate_id.



7
8
9
# File 'lib/aggregates/auditor.rb', line 7

def aggregate_id
  @aggregate_id
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/aggregates/auditor.rb', line 7

def type
  @type
end

Instance Method Details

#commandsObject

Returns all commands for a given aggregate.



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

def commands
  @commands ||= @storage_backend.load_commands_by_aggregate_id(@aggregate_id)
end

#commands_processed_after(time) ⇒ Object



41
42
43
# File 'lib/aggregates/auditor.rb', line 41

def commands_processed_after(time)
  commands.select { |event| event.created_at > time }
end

#commands_processed_by(time) ⇒ Object



37
38
39
# File 'lib/aggregates/auditor.rb', line 37

def commands_processed_by(time)
  commands.select { |event| event.created_at < time }
end

#eventsObject

Returns all stored events for a given aggregate.



24
25
26
# File 'lib/aggregates/auditor.rb', line 24

def events
  @events ||= @storage_backend.load_events_by_aggregate_id(@aggregate_id)
end

#events_processed_after(time) ⇒ Object



45
46
47
# File 'lib/aggregates/auditor.rb', line 45

def events_processed_after(time)
  events.select { |event| event.created_at > time }
end

#events_processed_by(time) ⇒ Object



33
34
35
# File 'lib/aggregates/auditor.rb', line 33

def events_processed_by(time)
  events.select { |event| event.created_at < time }
end

#inspect_state_at(time) ⇒ Object

This method creates a new instance of the aggregate root and replays the events on the aggregate alone. Only events that happened prior to the time specified are processed.



18
19
20
21
# File 'lib/aggregates/auditor.rb', line 18

def inspect_state_at(time)
  aggregate_repository = AggregateRepository.new(@storage_backend)
  aggregate_repository.load_aggregate(@type, @aggregate_id, at: time)
end