Skip to content

Commit

Permalink
class_events are broken up into stateless_events and constructor_events
Browse files Browse the repository at this point in the history
  • Loading branch information
hallgren committed Mar 30, 2016
1 parent 5bf970f commit d254642
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 155 deletions.
58 changes: 29 additions & 29 deletions lib/sandthorn/aggregate_root_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,35 +131,6 @@ def aggregate_build events
aggregate
end

def class_events(*event_names)
event_names.each do |name|
define_singleton_method name do |aggregate_id = nil, *args, &block|

unless aggregate_id #new aggregate
a = 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, args
return aggregate
end
else
event = build_event(name.to_s, args, [], nil)
#event[:event_data] = Sandthorn.serialize event[:event_args]
#event[:event_args] = nil

Sandthorn.save_events([event],
aggregate_id,
self)

return aggregate_id
end
end
self.singleton_class.class_eval { private name.to_s }
end
end

def build_event name, args, attribute_deltas, aggregate_version, trace_information = nil

data = {
Expand All @@ -180,6 +151,35 @@ def build_event name, args, attribute_deltas, aggregate_version, trace_informati

end

def stateless_events(*event_names)
event_names.each do |name|
define_singleton_method name do |aggregate_id, *args|
event = build_event(name.to_s, args, [], nil)
Sandthorn.save_events([event], aggregate_id, self)
return aggregate_id

end
end
end

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, args
return aggregate
end

end
self.singleton_class.class_eval { private name.to_s }
end
end

def events(*event_names)
event_names.each do |name|
define_method(name) do |*args, &block|
Expand Down
126 changes: 0 additions & 126 deletions spec/aggregate_class_events_spec.rb

This file was deleted.

64 changes: 64 additions & 0 deletions spec/constructor_events_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'spec_helper'

module Sandthorn
class ConstructorEventsSpec
include AggregateRoot

constructor_events :created_event
attr_reader :name


def self.create name
created_event(name) { @name = name }
end

end

describe "::constructor_events" do

let(:subject) do
ConstructorEventsSpec.create("name").save
end

context "interface" do

it "should not expose constructor_events methods" do
expect(subject).not_to respond_to(:created_event)
end

it "should create the constructor event on the class" do
expect(ConstructorEventsSpec.private_methods).to include(:created_event)
end

end
end

describe "::create" do
let(:aggregate_id) do
a = ConstructorEventsSpec.create("create_name")
a.save
a.aggregate_id
end

it "should create an ConstructorEventsSpec aggregate" do
expect(ConstructorEventsSpec.find(aggregate_id)).to be_a ConstructorEventsSpec
end

it "should set instance variable in aggregate" do
expect(ConstructorEventsSpec.find(aggregate_id).name).to eql "create_name"
end

it "should have created an created_event" do
expect(Sandthorn.get_aggregate_events(ConstructorEventsSpec, aggregate_id).first[:event_name]).to eql "created_event"
end

it "should have set the method_args to create_name" do
expect(Sandthorn.get_aggregate_events(ConstructorEventsSpec, aggregate_id).first[:event_args][:method_args].first).to eql "create_name"
end

it "should have set the attribute_delta name" do
expect(Sandthorn.get_aggregate_events(ConstructorEventsSpec, aggregate_id).first[:event_args][:attribute_deltas].last[:attribute_name]).to eql "name"
expect(Sandthorn.get_aggregate_events(ConstructorEventsSpec, aggregate_id).first[:event_args][:attribute_deltas].last[:new_value]).to eql "create_name"
end
end
end
98 changes: 98 additions & 0 deletions spec/stateless_events_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'spec_helper'

module Sandthorn
class StatelessEventsSpec
include AggregateRoot

stateless_events :one_event, :some_other_event
attr_reader :name

def initialize name
@name = name
end

# def self.call_one_event aggregate_id, hash = {}, value = 0
# one_event(aggregate_id, hash, value)
# end

end

describe "::stateless_events" do

context "interface" do

it "should expose stateless_events methods" do
expect(StatelessEventsSpec).to respond_to(:one_event)
end

end



context "when adding a stateless event to an existing aggregate" do

let(:subject) do
StatelessEventsSpec.new("name").save
end

before do
StatelessEventsSpec.one_event(subject.aggregate_id, args, 1)
end

let(:args) do
{first: "first", other: [1,2,3]}
end

let(:last_event) do
Sandthorn.get_aggregate_events(StatelessEventsSpec, subject.aggregate_id).last
end

let(:reloaded_subject) do
StatelessEventsSpec.find subject.aggregate_id
end

it "should add one_event last on the aggregate" do
expect(last_event[:event_name]).to eql "one_event"
end

it "should not have any deltas in event" do
expect(last_event[:event_args][:attribute_deltas]).to eql []
end

it "should store event arguments" do
expect(last_event[:event_args][:method_args].first).to eql(args)
expect(last_event[:event_args][:method_args].last).to eql(1)
end

it "should have same name attribute after reload" do
expect(subject.name).to eql(reloaded_subject.name)
end
end

context "when adding stateless_events to none existing aggregate" do

before do
StatelessEventsSpec.one_event(aggregate_id, "argument_data")
end

let(:aggregate_id) {"none_existing_aggregate_id"}

let(:events) do
Sandthorn.get_aggregate_events(StatelessEventsSpec, aggregate_id)
end

it "should store the stateless event as the first event" do
expect(events.length).to be 1
end

it "should have correct aggregate_id in event" do
expect(events.first[:aggregate_id]).to eql aggregate_id
end

it "should have event name one_event" do
expect(events.first[:event_name]).to eql "one_event"
end
end

end
end

0 comments on commit d254642

Please sign in to comment.