class Akasha::Aggregate

CQRS Aggregate base class.

Usage:

class User < Akasha::Aggregate

def sign_up(email, password)
  changeset.append(:user_signed_up, email: email, password: password)
end

def on_user_signed_up(email:, password:, **_)
  @email = email
  @password = password
end

end

Attributes

changeset[R]
revision[R]

Public Class Methods

new(id) click to toggle source
# File lib/akasha/aggregate.rb, line 24
def initialize(id)
  @revision = -1 # No stream exists.
  @changeset = Changeset.new(id)
end

Public Instance Methods

apply_events(events) click to toggle source

Replay events, building up the state of the aggregate. Used by Repository.

# File lib/akasha/aggregate.rb, line 31
def apply_events(events)
  events.each do |event|
    method_name = event_handler(event)
    public_send(method_name, event.data) if respond_to?(method_name)
  end
  @revision = events.last&.revision.to_i
end

Private Instance Methods

event_handler(event) click to toggle source
# File lib/akasha/aggregate.rb, line 41
def event_handler(event)
  :"on_#{event.name}"
end