Class: Sequent::Core::AggregateRoot

Inherits:
Object
  • Object
show all
Includes:
Helpers::SelfApplier
Defined in:
lib/sequent/core/aggregate_root.rb

Overview

Base class for all your domain classes.

load_from_history functionality to be loaded_from_history, meaning a stream of events.

Direct Known Subclasses

TenantAggregateRoot

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::SelfApplier

#handle_message, included

Constructor Details

#initialize(id) ⇒ AggregateRoot

Returns a new instance of AggregateRoot.



20
21
22
23
24
# File 'lib/sequent/core/aggregate_root.rb', line 20

def initialize(id)
  @id = id
  @uncommitted_events = []
  @sequence_number = 1
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/sequent/core/aggregate_root.rb', line 12

def id
  @id
end

#sequence_numberObject (readonly)

Returns the value of attribute sequence_number.



12
13
14
# File 'lib/sequent/core/aggregate_root.rb', line 12

def sequence_number
  @sequence_number
end

#uncommitted_eventsObject (readonly)

Returns the value of attribute uncommitted_events.



12
13
14
# File 'lib/sequent/core/aggregate_root.rb', line 12

def uncommitted_events
  @uncommitted_events
end

Class Method Details

.load_from_history(events) ⇒ Object



14
15
16
17
18
# File 'lib/sequent/core/aggregate_root.rb', line 14

def self.load_from_history(events)
  aggregate_root = allocate() # allocate without calling new
  aggregate_root.load_from_history(events)
  aggregate_root
end

Instance Method Details

#clear_eventsObject



39
40
41
# File 'lib/sequent/core/aggregate_root.rb', line 39

def clear_events
  uncommitted_events.clear
end

#load_from_history(events) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/sequent/core/aggregate_root.rb', line 26

def load_from_history(events)
  raise "Empty history" if events.empty?
  @id = events.first.aggregate_id
  @uncommitted_events = []
  @sequence_number = events.size + 1
  events.each { |event| handle_message(event) }
end

#to_sObject



34
35
36
# File 'lib/sequent/core/aggregate_root.rb', line 34

def to_s
  "#{self.class.name}: #{@id}"
end