Class: Sequent::Core::AggregateRoot
- Inherits:
-
Object
- Object
- Sequent::Core::AggregateRoot
- 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.
Instance Attribute Summary collapse
-
#event_stream ⇒ Object
readonly
Returns the value of attribute event_stream.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#sequence_number ⇒ Object
readonly
Returns the value of attribute sequence_number.
-
#uncommitted_events ⇒ Object
readonly
Returns the value of attribute uncommitted_events.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_event(event) ⇒ Object
- #clear_events ⇒ Object
-
#initialize(id) ⇒ AggregateRoot
constructor
A new instance of AggregateRoot.
- #load_from_history(stream, events) ⇒ Object
- #take_snapshot! ⇒ Object
- #to_s ⇒ Object
Methods included from SnapshotConfiguration
Methods included from Helpers::AutosetAttributes
Methods included from Helpers::MessageHandler
Constructor Details
#initialize(id) ⇒ AggregateRoot
Returns a new instance of AggregateRoot.
55 56 57 58 59 60 61 62 |
# File 'lib/sequent/core/aggregate_root.rb', line 55 def initialize(id) @id = id @uncommitted_events = [] @sequence_number = 1 @event_stream = EventStream.new aggregate_type: self.class.name, aggregate_id: id, snapshot_threshold: self.class.snapshot_default_threshold end |
Instance Attribute Details
#event_stream ⇒ Object (readonly)
Returns the value of attribute event_stream.
41 42 43 |
# File 'lib/sequent/core/aggregate_root.rb', line 41 def event_stream @event_stream end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
41 42 43 |
# File 'lib/sequent/core/aggregate_root.rb', line 41 def id @id end |
#sequence_number ⇒ Object (readonly)
Returns the value of attribute sequence_number.
41 42 43 |
# File 'lib/sequent/core/aggregate_root.rb', line 41 def sequence_number @sequence_number end |
#uncommitted_events ⇒ Object (readonly)
Returns the value of attribute uncommitted_events.
41 42 43 |
# File 'lib/sequent/core/aggregate_root.rb', line 41 def uncommitted_events @uncommitted_events end |
Class Method Details
.load_from_history(stream, events) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sequent/core/aggregate_root.rb', line 43 def self.load_from_history(stream, events) first, *rest = events if first.is_a? SnapshotEvent aggregate_root = Marshal.load(Base64.decode64(first.data)) rest.each { |x| aggregate_root.apply_event(x) } else aggregate_root = allocate() # allocate without calling new aggregate_root.load_from_history(stream, events) end aggregate_root end |
Instance Method Details
#apply_event(event) ⇒ Object
86 87 88 89 |
# File 'lib/sequent/core/aggregate_root.rb', line 86 def apply_event(event) (event) @sequence_number = event.sequence_number + 1 end |
#clear_events ⇒ Object
77 78 79 |
# File 'lib/sequent/core/aggregate_root.rb', line 77 def clear_events @uncommitted_events = [] end |
#load_from_history(stream, events) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/sequent/core/aggregate_root.rb', line 64 def load_from_history(stream, events) raise "Empty history" if events.empty? @id = events.first.aggregate_id @uncommitted_events = [] @sequence_number = 1 @event_stream = stream events.each { |event| apply_event(event) } end |
#take_snapshot! ⇒ Object
81 82 83 84 |
# File 'lib/sequent/core/aggregate_root.rb', line 81 def take_snapshot! snapshot = build_event SnapshotEvent, data: Base64.encode64(Marshal.dump(self)) @uncommitted_events << snapshot end |
#to_s ⇒ Object
73 74 75 |
# File 'lib/sequent/core/aggregate_root.rb', line 73 def to_s "#{self.class.name}: #{@id}" end |