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


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

def initialize(id)
  @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

Instance Method Details

#apply_events(events) ⇒ Object

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


30
31
32
33
34
35
# File 'lib/akasha/aggregate.rb', line 30

def apply_events(events)
  events.each do |event|
    method_name = event_handler(event)
    send(method_name, event.data) if respond_to?(method_name)
  end
end