module SnapshotAggregateRoot

Constants

MissingHandler
VERSION

Attributes

configuration[RW]
events_since_snapshot[RW]
loaded_from_stream_name[R]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/snapshot_aggregate_root/configuration.rb, line 6
def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

Public Instance Methods

apply(event) click to toggle source
# File lib/snapshot_aggregate_root.rb, line 9
def apply(event)
  apply_strategy.(self, event)
  unpublished_events << event
end
load(stream_name, event_store: default_event_store) click to toggle source
# File lib/snapshot_aggregate_root.rb, line 27
def load(stream_name, event_store: default_event_store)
  @loaded_from_stream_name = stream_name

  snapshot = event_store.last_stream_snapshot(stream_name)
  if snapshot
    apply_snapshot(snapshot)
    events = event_store.read_events_forward(stream_name, start: snapshot.event_id, count: 0)
  else
    events = event_store.read_events_forward(stream_name, count: 0)
  end

  events.each(&method(:apply))
  self.events_since_snapshot = events.count
  @unpublished_events = nil
  self
end
notify(event_store: ) click to toggle source
# File lib/snapshot_aggregate_root.rb, line 56
def notify(event_store: )
  unnotified_events.each do |event|
    event_store.notify_subscribers(event)
  end
  @unnotified_events = nil
end
store(stream_name = loaded_from_stream_name, event_store: default_event_store) click to toggle source
# File lib/snapshot_aggregate_root.rb, line 44
def store(stream_name = loaded_from_stream_name, event_store: default_event_store)
  self.events_since_snapshot += @unpublished_events.count
  unpublished_events.each do |event|
    event_store.append_to_stream(event, stream_name: stream_name)
    unnotified_events.push(event)
  end
  @unpublished_events = nil
  if requires_snapshot?
    snapshot!(stream_name, event_store: event_store)
  end
end
with_lock(stream_name, event_store: default_event_store, &block) click to toggle source
# File lib/snapshot_aggregate_root.rb, line 14
def with_lock(stream_name, event_store: default_event_store, &block)
  event_store.with_lock(stream_name, &block)
end
with_write_context(stream_name, event_store: default_event_store) { |self| ... } click to toggle source
# File lib/snapshot_aggregate_root.rb, line 18
def with_write_context(stream_name, event_store: default_event_store)
  with_lock(stream_name, event_store: event_store) do
    load(stream_name, event_store: event_store)
    yield self
    store(stream_name, event_store: event_store)
  end
  notify(event_store: event_store)
end

Private Instance Methods

apply_snapshot(snapshot) click to toggle source

This method must be implemented by consumers of this module

Returns an Event

# File lib/snapshot_aggregate_root.rb, line 81
def apply_snapshot(snapshot)
  raise "apply_snapshot not implemented in #{self}"
end
apply_strategy() click to toggle source
# File lib/snapshot_aggregate_root.rb, line 106
def apply_strategy
  DefaultApplyStrategy.new
end
build_snapshot() click to toggle source

This method must be implemented by consumers of this module

Returns an Event

# File lib/snapshot_aggregate_root.rb, line 74
def build_snapshot
  raise "build_snapshot not implemented in #{self}"
end
default_event_store() click to toggle source
# File lib/snapshot_aggregate_root.rb, line 110
def default_event_store
  SnapshotAggregateRoot.configuration.default_event_store
end
requires_snapshot?() click to toggle source
# File lib/snapshot_aggregate_root.rb, line 85
def requires_snapshot?
  events_since_snapshot >= snapshot_threshold
end
snapshot!(stream_name, event_store:) click to toggle source
# File lib/snapshot_aggregate_root.rb, line 93
def snapshot!(stream_name, event_store:)
  event = build_snapshot
  event_store.publish_snapshot(event, stream_name: stream_name)
end
snapshot_threshold() click to toggle source
# File lib/snapshot_aggregate_root.rb, line 89
def snapshot_threshold
  50
end
unnotified_events() click to toggle source
# File lib/snapshot_aggregate_root.rb, line 102
def unnotified_events
  @unnotified_events ||= []
end
unpublished_events() click to toggle source
# File lib/snapshot_aggregate_root.rb, line 98
def unpublished_events
  @unpublished_events ||= []
end