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,



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

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



37
38
39
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 37

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

#find(id) ⇒ Object

Find by database table id.



22
23
24
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 22

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

#find_by_aggregate_id(aggregate_id) ⇒ Object



26
27
28
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 26

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.



31
32
33
34
35
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 31

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



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

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.



16
17
18
19
# File 'lib/sandthorn_driver_sequel/access/aggregate_access.rb', line 16

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