class Eventusha::Aggregate

Attributes

aggregate_id[R]

Public Class Methods

build_from(events) click to toggle source
# File lib/eventusha/aggregate.rb, line 24
def self.build_from(events)
  object = self.new
  return object if events.blank?

  events.each do |event|
    event = event.becomes(event.name.constantize)
    object.apply(event, published: true)
  end

  object
end
find(aggregate_id) click to toggle source
# File lib/eventusha/aggregate.rb, line 13
def self.find(aggregate_id)
  events = Event.where(aggregate_id: aggregate_id)
  build_from(events)
end
on(*event_classes, &block) click to toggle source
# File lib/eventusha/aggregate.rb, line 5
def self.on(*event_classes, &block)
  event_classes.each do |event_class|
    handler_name = "on_#{event_class.name.demodulize.underscore}"
    define_method(handler_name, block)
    private(handler_name)
  end
end

Public Instance Methods

apply(event, published: false) click to toggle source
# File lib/eventusha/aggregate.rb, line 18
def apply(event, published: false)
  create_event(event) unless published

  send(apply_event_method_name(event), event)
end

Private Instance Methods

apply_event_method_name(event) click to toggle source
# File lib/eventusha/aggregate.rb, line 38
def apply_event_method_name(event)
  "on_#{event.method_name}"
end
create_event(event) click to toggle source
# File lib/eventusha/aggregate.rb, line 42
def create_event(event)
  event.save
  event.publish
end