class Synapse::EventSourcing::AggregateSnapshotTaker

Snapshot taker that uses the actual aggregate and its state to create a snapshot event

Public Class Methods

new() click to toggle source

@return [undefined]

Calls superclass method
# File lib/synapse/event_sourcing/snapshot/aggregate_taker.rb, line 6
def initialize
  super
  @aggregate_factories = Hash.new
end

Public Instance Methods

register_factory(factory) click to toggle source

@param [AggregateFactory] factory @return [undefined]

# File lib/synapse/event_sourcing/snapshot/aggregate_taker.rb, line 13
def register_factory(factory)
  @aggregate_factories.store factory.type_identifier, factory
end

Protected Instance Methods

create_snapshot(type_identifier, aggregate_id, stream) click to toggle source

@param [String] type_identifier @param [Object] aggregate_id @param [DomainEventStream] stream @return [DomainEventMessage]

# File lib/synapse/event_sourcing/snapshot/aggregate_taker.rb, line 23
def create_snapshot(type_identifier, aggregate_id, stream)
  factory = @aggregate_factories.fetch type_identifier

  aggregate = factory.create_aggregate aggregate_id, stream.peek
  aggregate.initialize_from_stream stream

  Domain::DomainEventMessage.build do |builder|
    builder.payload = aggregate
    builder.aggregate_id = aggregate.id
    builder.sequence_number = aggregate.version
  end
end