Sha256: 0204a01b5c7dc4d831cbd5a156d9d353dbfdc714d0c54ae5741cb44cc4ededdc

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

module SandthornDriverSequel
  class AggregateAccess < Access::Base

    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

    # Create a database row for an aggregate.
    # Return the row.
    def register_aggregate(aggregate_id, aggregate_type)
      id = storage.aggregates.insert(aggregate_id: aggregate_id, aggregate_type: aggregate_type.to_s)
      find(id)
    end

    # Find by database table id.
    def find(id)
      storage.aggregates[id]
    end

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

    # Throws an error if the aggregate isn't registered.
    def find_by_aggregate_id!(aggregate_id)
      aggregate = find_by_aggregate_id(aggregate_id)
      raise Errors::NoAggregateError, aggregate_id unless aggregate
      aggregate
    end

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

    # Returns aggregate ids.
    # @param aggregate_type, optional,
    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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sandthorn_driver_sequel-2.1.1 lib/sandthorn_driver_sequel/access/aggregate_access.rb
sandthorn_driver_sequel-2.1.0 lib/sandthorn_driver_sequel/access/aggregate_access.rb
sandthorn_driver_sequel-2.0.1 lib/sandthorn_driver_sequel/access/aggregate_access.rb
sandthorn_driver_sequel-2.0.0 lib/sandthorn_driver_sequel/access/aggregate_access.rb