Module: Synapse::EventSourcing::AggregateRoot

Extended by:
ActiveSupport::Concern
Includes:
Domain::AggregateRoot, Member
Defined in:
lib/synapse/event_sourcing/aggregate_root.rb

Overview

Mixin for the root entity of an aggregate that is initialized from a historical event stream

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary

Attributes included from Domain::AggregateRoot

#deleted, #id

Instance Method Summary collapse

Methods included from Domain::AggregateRoot

#add_registration_listener, #mark_committed, #uncommitted_event_count, #uncommitted_events

Instance Method Details

#handle_member_event(payload, metadata = nil) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Called when a member of the aggregate publishes an event

This is only meant to be invoked by entities that are members of this aggregate

Parameters:

  • payload (Object)
  • metadata (Hash) (defaults to: nil)

Returns:

  • (undefined)


57
58
59
# File 'lib/synapse/event_sourcing/aggregate_root.rb', line 57

def handle_member_event(payload,  = nil)
  apply payload, 
end

#initialize_from_stream(stream) ⇒ undefined

Initializes the state of this aggregate from the given domain event stream

Parameters:

  • stream (DomainEventStream)

Returns:

  • (undefined)

Raises:

  • (RuntimeError)

    If aggregate has already been initialized



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/synapse/event_sourcing/aggregate_root.rb', line 32

def initialize_from_stream(stream)
  if uncommitted_event_count > 0
    raise 'Aggregate has already been initialized'
  end

  pre_initialize

  last_sequence_number = nil

  stream.each do |event|
    last_sequence_number = event.sequence_number
    handle_recursively event
  end

  initialize_event_container last_sequence_number
end

#versionInteger

Returns The sequence number of the last committed event.

Returns:

  • (Integer)

    The sequence number of the last committed event



23
24
25
# File 'lib/synapse/event_sourcing/aggregate_root.rb', line 23

def version
  last_committed_sequence_number
end