Class: Akasha::Aggregate

Inherits:
Object
  • Object
show all
Includes:
SyntaxHelpers
Defined in:
lib/akasha/aggregate.rb

Overview

CQRS Aggregate base class.

Usage:

class User < Akasha::Aggregate

def (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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SyntaxHelpers

included

Constructor Details

#initialize(id) ⇒ Aggregate

Returns a new instance of Aggregate.



24
25
26
27
# File 'lib/akasha/aggregate.rb', line 24

def initialize(id)
  @revision = -1 # No stream exists.
  @changeset = Changeset.new(id)
end

Instance Attribute Details

#changesetObject (readonly)

Returns the value of attribute changeset.



22
23
24
# File 'lib/akasha/aggregate.rb', line 22

def changeset
  @changeset
end

#revisionObject (readonly)

Returns the value of attribute revision.



22
23
24
# File 'lib/akasha/aggregate.rb', line 22

def revision
  @revision
end

Instance Method Details

#apply_events(events) ⇒ Object

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



31
32
33
34
35
36
37
# 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