Module: SnapshotAggregateRoot

Defined in:
lib/snapshot_aggregate_root.rb,
lib/snapshot_aggregate_root/version.rb,
lib/snapshot_aggregate_root/configuration.rb,
lib/snapshot_aggregate_root/default_apply_strategy.rb

Defined Under Namespace

Classes: Configuration, DefaultApplyStrategy

Constant Summary collapse

VERSION =
'0.5.0'
MissingHandler =
Class.new(StandardError)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



3
4
5
# File 'lib/snapshot_aggregate_root/configuration.rb', line 3

def configuration
  @configuration
end

Instance Attribute Details

#events_since_snapshotObject

Returns the value of attribute events_since_snapshot.



7
8
9
# File 'lib/snapshot_aggregate_root.rb', line 7

def events_since_snapshot
  @events_since_snapshot
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



6
7
8
9
# File 'lib/snapshot_aggregate_root/configuration.rb', line 6

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

Instance Method Details

#apply(event) ⇒ Object



9
10
11
12
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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:) ⇒ Object



56
57
58
59
60
61
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



14
15
16
# 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) ⇒ Object



18
19
20
21
22
23
24
25
# 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