Class: Synapse::EventSourcing::GenericAggregateFactory

Inherits:
AggregateFactory show all
Defined in:
lib/synapse/event_sourcing/aggregate_factory.rb

Overview

Aggregate factory that uses a convention to create instances of aggregates

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aggregate_type) ⇒ undefined

Parameters:

  • aggregate_type (Class)


40
41
42
43
# File 'lib/synapse/event_sourcing/aggregate_factory.rb', line 40

def initialize(aggregate_type)
  @aggregate_type = aggregate_type
  @type_identifier = aggregate_type.to_s.demodulize
end

Instance Attribute Details

#aggregate_typeClass (readonly)

Returns:

  • (Class)


33
34
35
# File 'lib/synapse/event_sourcing/aggregate_factory.rb', line 33

def aggregate_type
  @aggregate_type
end

#type_identifierString (readonly)

Returns:

  • (String)


36
37
38
# File 'lib/synapse/event_sourcing/aggregate_factory.rb', line 36

def type_identifier
  @type_identifier
end

Instance Method Details

#create_aggregate(aggregate_id, first_event) ⇒ AggregateRoot

Parameters:

  • aggregate_id (Object)
  • first_event (DomainEventMessage)

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/synapse/event_sourcing/aggregate_factory.rb', line 48

def create_aggregate(aggregate_id, first_event)
  payload = first_event.payload

  if payload.is_a? AggregateRoot
    aggregate = payload
    aggregate.reset_initial_version
  else
    aggregate = @aggregate_type.allocate
  end

  post_process aggregate
end