module Sandthorn::AggregateRoot::Base::ClassMethods

Public Instance Methods

aggregate_build(events, aggregate_from_snapshot = nil) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 130
def aggregate_build events, aggregate_from_snapshot = nil
  aggregate = aggregate_from_snapshot || create_new_empty_aggregate

  if events.any?
    current_aggregate_version = events.last[:aggregate_version]
    aggregate.send :set_orginating_aggregate_version!, current_aggregate_version
    aggregate.send :set_current_aggregate_version!, current_aggregate_version
    aggregate.send :set_aggregate_id, events.first.fetch(:aggregate_id)
  end
  attributes = build_instance_vars_from_events events
  aggregate.send :clear_aggregate_events

  aggregate.default_attributes
  aggregate.send :aggregate_initialize

  aggregate.send :set_instance_variables!, attributes
  aggregate
end
aggregate_find(aggregate_id) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 98
def aggregate_find aggregate_id
  begin
    aggregate_from_snapshot = Sandthorn.find_snapshot(aggregate_id) if self.snapshot
    current_aggregate_version = aggregate_from_snapshot.nil? ? 0 : aggregate_from_snapshot.aggregate_current_event_version
    events = Sandthorn.find(aggregate_id, self, current_aggregate_version)
    if aggregate_from_snapshot.nil? && events.empty?
      raise Errors::AggregateNotFound
    end

    return aggregate_build events, aggregate_from_snapshot
  rescue Exception
    raise Errors::AggregateNotFound
  end
    
end
aggregate_trace(args) { |self| ... } click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 65
def aggregate_trace args
  @@aggregate_trace_information = args
  yield self
  @@aggregate_trace_information = nil
end
all() click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 87
def all
  Sandthorn.all(self).map { |events|
    aggregate_build events, nil
  }
end
constructor_events(*event_names) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 159
def constructor_events(*event_names)
  event_names.each do |name|
    define_singleton_method name do |*args, &block|

      create_new_empty_aggregate.tap  do |aggregate|
        aggregate.aggregate_base_initialize
        aggregate.aggregate_initialize
        aggregate.send :set_aggregate_id, Sandthorn.generate_aggregate_id
        aggregate.instance_eval(&block) if block
        aggregate.send :commit_with_event_name, name.to_s
        return aggregate
      end

    end
    self.singleton_class.class_eval { private name.to_s }
  end
end
event_store(event_store = nil) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 71
def event_store(event_store = nil)
  if event_store
    @event_store = event_store
  else
    @event_store
  end
end
events(*event_names) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 177
def events(*event_names)
  event_names.each do |name|
    define_method(name) do |*args, &block|
      block.call() if block
      commit_with_event_name(name.to_s)
    end
    private name.to_s
  end
end
find(id) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 93
def find id
  return aggregate_find id unless id.respond_to?(:each)
  return id.map { |e| aggregate_find e }
end
new(*args, &block) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 114
def new *args, &block
  aggregate = create_new_empty_aggregate()
  aggregate.aggregate_base_initialize
  aggregate.aggregate_initialize

  aggregate.default_attributes
  aggregate.send :initialize, *args, &block
  aggregate.send :set_aggregate_id, Sandthorn.generate_aggregate_id

  aggregate.aggregate_trace @@aggregate_trace_information do |aggr|
    aggr.send :commit
    return aggr
  end

end
snapshot(value = nil) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 79
def snapshot(value = nil)
  if value
    @snapshot = value
  else
    @snapshot
  end
end
stateless_events(*event_names) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 149
def stateless_events(*event_names)
  event_names.each do |name|
    define_singleton_method name do |aggregate_id, *args|
      event = build_stateless_event(aggregate_id, name.to_s, args)
      Sandthorn.save_events([event], aggregate_id, self)
      return aggregate_id
    end
  end
end

Private Instance Methods

build_instance_vars_from_events(events) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 206
def build_instance_vars_from_events events
  events.each_with_object({}) do |event, instance_vars|
    attribute_deltas = event[:event_data]
    unless attribute_deltas.nil?
      deltas = {}
      attribute_deltas.each do |key, value|
        deltas[key] = value[:new_value]
      end
      instance_vars.merge! deltas
    end
  end
end
build_stateless_event(aggregate_id, name, args = []) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 189
def build_stateless_event aggregate_id, name, args = []

  deltas = {}
  args.first.each do |key, value|
    deltas[key.to_sym] = { old_value: nil, new_value: value }
  end unless args.empty?

  return {
    aggregate_version: nil,
    aggregate_id: aggregate_id,
    event_name: name,
    event_data: deltas,
    event_metadata: nil
  }

end
create_new_empty_aggregate() click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 219
def create_new_empty_aggregate
  allocate
end