Class: Lieutenant::AggregateRepository::AggregateRepositoryUnit

Inherits:
Object
  • Object
show all
Defined in:
lib/lieutenant/aggregate_repository.rb

Overview

Represents one unit of work of the repository to grant independence between multiple concurrent commands being handled

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ AggregateRepositoryUnit

Returns a new instance of AggregateRepositoryUnit.



21
22
23
24
# File 'lib/lieutenant/aggregate_repository.rb', line 21

def initialize(store)
  @aggregates = {}
  @store = store
end

Instance Method Details

#add_aggregate(aggregate) ⇒ Object



26
27
28
# File 'lib/lieutenant/aggregate_repository.rb', line 26

def add_aggregate(aggregate)
  aggregates[aggregate.id] = aggregate
end

#executeObject



37
38
39
40
41
42
# File 'lib/lieutenant/aggregate_repository.rb', line 37

def execute
  yield(self)
  commit
ensure
  clean
end

#load_aggregate(aggregate_type, aggregate_id) ⇒ Object



30
31
32
33
34
35
# File 'lib/lieutenant/aggregate_repository.rb', line 30

def load_aggregate(aggregate_type, aggregate_id)
  aggregates[aggregate_id] ||= begin
    history = store.event_stream_for(aggregate_id)
    aggregate_type.load_from_history(aggregate_id, history)
  end
end