Class: Synapse::EventSourcing::AggregateSnapshotTaker
- Inherits:
-
SnapshotTaker
- Object
- SnapshotTaker
- Synapse::EventSourcing::AggregateSnapshotTaker
- Defined in:
- lib/synapse/event_sourcing/snapshot/taker.rb
Overview
Snapshot taker that uses the actual aggregate and its state to create a snapshot event
Instance Method Summary collapse
- #initialize(event_store) ⇒ undefined constructor
- #register_factory(factory) ⇒ undefined
- #schedule_snapshot(type_identifier, aggregate_id) ⇒ undefined
Constructor Details
#initialize(event_store) ⇒ undefined
24 25 26 27 |
# File 'lib/synapse/event_sourcing/snapshot/taker.rb', line 24 def initialize(event_store) @aggregate_factories = Hash.new @event_store = event_store end |
Instance Method Details
#register_factory(factory) ⇒ undefined
31 32 33 |
# File 'lib/synapse/event_sourcing/snapshot/taker.rb', line 31 def register_factory(factory) @aggregate_factories.store factory.type_identifier, factory end |
#schedule_snapshot(type_identifier, aggregate_id) ⇒ undefined
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/synapse/event_sourcing/snapshot/taker.rb', line 38 def schedule_snapshot(type_identifier, aggregate_id) stream = @event_store.read_events type_identifier, aggregate_id factory = @aggregate_factories.fetch type_identifier aggregate = factory.create_aggregate aggregate_id, stream.peek aggregate.initialize_from_stream stream snapshot = Domain::DomainEventMessage.build do |builder| builder.payload = aggregate builder.aggregate_id = aggregate.id builder.sequence_number = aggregate.version end @event_store.append_snapshot_event type_identifier, snapshot end |