Class: SandthornDriverSequel2::SnapshotAccess

Inherits:
Access::Base
  • Object
show all
Defined in:
lib/sandthorn_driver_sequel_2/access/snapshot_access.rb

Instance Method Summary collapse

Methods inherited from Access::Base

#initialize

Constructor Details

This class inherits a constructor from SandthornDriverSequel2::Access::Base

Instance Method Details

#find(snapshot_id) ⇒ Object



9
10
11
# File 'lib/sandthorn_driver_sequel_2/access/snapshot_access.rb', line 9

def find(snapshot_id)
  storage.snapshots[snapshot_id]
end

#find_by_aggregate_id(aggregate_id) ⇒ Object



4
5
6
7
# File 'lib/sandthorn_driver_sequel_2/access/snapshot_access.rb', line 4

def find_by_aggregate_id(aggregate_id)
  #aggregate = aggregates.find_by_aggregate_id(aggregate_id)
  storage.snapshots.first(aggregate_id: aggregate_id)
end

#obsolete(aggregate_types: [], max_event_distance: 100) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sandthorn_driver_sequel_2/access/snapshot_access.rb', line 21

def obsolete(aggregate_types: [], max_event_distance: 100)
  aggregate_types.map!(&:to_s)
  snapshot_version = Sequel.qualify(storage.snapshots_table_name, :aggregate_version)
  aggregate_version = Sequel.qualify(storage.aggregates_table_name, :aggregate_version)
  aggregate_id = Sequel.qualify(storage.aggregates_table_name, :aggregate_id)
  query = storage.aggregates.left_outer_join(storage.snapshots, aggregate_id: :aggregate_id)
  query = query.select { (aggregate_version - snapshot_version).as(distance) }
  query = query.select_append(aggregate_id, :aggregate_type)
  query = query.where { (aggregate_version - coalesce(snapshot_version, 0)) > max_event_distance }
  if aggregate_types.any?
    query = query.where(aggregate_type: aggregate_types)
  end
  query.all
end

#record_snapshot(aggregate_id, snapshot_data) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/sandthorn_driver_sequel_2/access/snapshot_access.rb', line 13

def record_snapshot(aggregate_id, snapshot_data)
  #aggregate = aggregates.find_by_aggregate_id!(aggregate_id)
  previous_snapshot = find_by_aggregate_id(aggregate_id)
  #if perform_snapshot?(aggregate, previous_snapshot)
  perform_snapshot(aggregate_id, previous_snapshot, snapshot_data)
  #end
end