class Sequent::Core::Projector

Projectors listen to events and update the view state as they see fit.

Example of updating view state, in this case the InvoiceRecord table representing an Invoice

class InvoiceProjector < Sequent::Core::Projector
  manages_tables InvoiceRecord

  on InvoiceCreated do |event|
    create_record(
      InvoiceRecord,
      recipient: event.recipient,
      amount: event.amount
    )
  end
end

Please note that the actual storage is abstracted away in the persistors. Due to this abstraction you can not traverse persist or traverse child objects like you are used to do with ActiveRecord. The following example will not work:

invoice_record.line_item_records << create_record(LineItemRecord, ...)

In this case you should simply do:

create_record(LineItemRecord, invoice_id: invoice_record.aggregate_id)

Attributes

abstract_class[RW]
skip_autoregister[RW]

Private Class Methods

new(persistor = Sequent::Core::Persistors::ActiveRecordPersistor.new) click to toggle source
# File lib/sequent/core/projector.rb, line 95
def initialize(persistor = Sequent::Core::Persistors::ActiveRecordPersistor.new)
  ensure_valid!
  @persistor = persistor
end
replay_persistor() click to toggle source
# File lib/sequent/core/projector.rb, line 100
def self.replay_persistor
  nil
end

Private Instance Methods

ensure_valid!() click to toggle source
# File lib/sequent/core/projector.rb, line 123
      def ensure_valid!
        return if self.class.manages_no_tables?

        if self.class.managed_tables.nil? || self.class.managed_tables.empty?
          fail <<~EOS.chomp
            A Projector must manage at least one table. Did you forget to add `managed_tables` to #{self.class.name}?
          EOS
        end
      end