Class: SandthornDriverSequel::AggregateAccess

Inherits:
SandthornDriverSequel::Access::Base show all
Defined in:
lib/sandthorn_driver_sequel/access/aggregate_access.rb

Instance Method Summary collapse

Methods inherited from SandthornDriverSequel::Access::Base

#initialize

Constructor Details

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

Instance Method Details

#aggregate_ids(aggregate_type: nil) ⇒ Object

Returns aggregate ids.

Parameters:

  • aggregate_type,

    optional,



41
42
43
44
45
46
47
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 41

def aggregate_ids(aggregate_type: nil)
  aggs = storage.aggregates
  if aggregate_type
    aggs = aggs.where(aggregate_type: aggregate_type.to_s)
  end
  aggs.order(:id).select_map(:aggregate_id)
end

#aggregate_typesObject



35
36
37
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 35

def aggregate_types
  storage.aggregates.select(:aggregate_type).distinct.select_map(:aggregate_type)
end

#find(id) ⇒ Object

Find by database table id.



20
21
22
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 20

def find(id)
  storage.aggregates[id]
end

#find_by_aggregate_id(aggregate_id) ⇒ Object



24
25
26
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 24

def find_by_aggregate_id(aggregate_id)
  storage.aggregates.first(aggregate_id: aggregate_id)
end

#find_by_aggregate_id!(aggregate_id) ⇒ Object

Throws an error if the aggregate isn’t registered.



29
30
31
32
33
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 29

def find_by_aggregate_id!(aggregate_id)
  aggregate = find_by_aggregate_id(aggregate_id)
  raise Errors::NoAggregateError, aggregate_id unless aggregate
  aggregate
end

#find_or_register(aggregate_id, aggregate_type) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 4

def find_or_register(aggregate_id, aggregate_type)
  if aggregate = find_by_aggregate_id(aggregate_id)
    aggregate
  else
    register_aggregate(aggregate_id, aggregate_type)
  end
end

#register_aggregate(aggregate_id, aggregate_type) ⇒ Object

Create a database row for an aggregate. Return the row.



14
15
16
17
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 14

def register_aggregate(aggregate_id, aggregate_type)
  id = storage.aggregates.insert(aggregate_id: aggregate_id, aggregate_type: aggregate_type.to_s)
  find(id)
end