Sha256: a6d3c5988e0c564972d760c38da07f74c9496ba30b1d9d3a50d70a63e86bdc60

Contents?: true

Size: 941 Bytes

Versions: 2

Compression:

Stored size: 941 Bytes

Contents

module Sandthorn
  module AggregateRootSnapshot
    attr_reader :aggregate_snapshot

    def snapshot
      aggregate_snapshot!
      save_snapshot
      self
    end

    def aggregate_snapshot!
      if @aggregate_events.count > 0
        raise Errors::SnapshotError,
          "Can't take snapshot on object with unsaved events"
      end

      @aggregate_snapshot = {
        event_name: "aggregate_set_from_snapshot",
        event_args: [self],
        aggregate_version: @aggregate_current_event_version
      }
    end

    def save_snapshot
      unless aggregate_snapshot
        raise Errors::SnapshotError, "No snapshot has been created!"
      end

      @aggregate_snapshot[:event_data] = Sandthorn
        .serialize aggregate_snapshot[:event_args]

      @aggregate_snapshot[:event_args] = nil
      Sandthorn.save_snapshot aggregate_snapshot, aggregate_id, self.class.name
      @aggregate_snapshot = nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sandthorn-0.5.1 lib/sandthorn/aggregate_root_snapshot.rb
sandthorn-0.5.0 lib/sandthorn/aggregate_root_snapshot.rb