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



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

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

Instance Attribute Details

#aggregate_typeClass (readonly)



29
30
31
# File 'lib/synapse/event_sourcing/aggregate_factory.rb', line 29

def aggregate_type
  @aggregate_type
end

#type_identifierString (readonly)



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

def type_identifier
  @type_identifier
end

Instance Method Details

#create_aggregate(aggregate_id, first_event) ⇒ AggregateRoot



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/synapse/event_sourcing/aggregate_factory.rb', line 44

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