Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event type resolver in aggregate root #969

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions aggregate_root/lib/aggregate_root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module OnDSL

def on(*event_klasses, &block)
event_klasses.each do |event_klass|
name = event_klass.to_s
name = event_type_for(event_klass)
raise(ArgumentError, "Anonymous class is missing name") if name.start_with? ANONYMOUS_CLASS

handler_name = "on_#{name}"
Expand Down Expand Up @@ -65,17 +65,29 @@ def unpublished_events
def self.with_default_apply_strategy
Module.new do
def self.included(host_class)
host_class.extend OnDSL
host_class.include AggregateRoot.with_strategy(->{ DefaultApplyStrategy.new })
warn <<~EOW
Please replace include AggregateRoot.with_default_apply_strategy with include AggregateRoot
EOW
host_class.include AggregateRoot
end
end
end

def self.with_strategy(strategy)
warn <<~EOW
Please replace include AggregateRoot.with_strategy(...) with include AggregateRoot.with(strategy: ...)
EOW
with(strategy: strategy)
end

def self.with(strategy: ->{ DefaultApplyStrategy.new }, event_type_resolver: ->(value) { value.to_s })
Module.new do
def self.included(host_class)
define_singleton_method :included do |host_class|
host_class.extend Constructor
host_class.include AggregateMethods
host_class.define_singleton_method :event_type_for do |value|
event_type_resolver.call(value)
end
end

define_method :apply_strategy do
Expand All @@ -85,6 +97,7 @@ def self.included(host_class)
end

def self.included(host_class)
host_class.include with_default_apply_strategy
host_class.extend OnDSL
host_class.include with
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that reads horrible :/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... naming is hard ;)

end
end
63 changes: 49 additions & 14 deletions aggregate_root/spec/aggregate_root_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,48 @@
expect { order.apply(spanish_inquisition) }.to raise_error(AggregateRoot::MissingHandler, "Missing handler method apply_spanish_inquisition on aggregate Order")
end

it "should raise error for missing apply method based on a default apply strategy (explicity stated)" do
klass = silence_warnings do
Class.new do
include AggregateRoot.with_default_apply_strategy
end
end
order = klass.new
spanish_inquisition = Orders::Events::SpanishInquisition.new
expect { order.apply(spanish_inquisition) }.to raise_error(AggregateRoot::MissingHandler, "Missing handler method apply_spanish_inquisition on aggregate #{klass}")
end

it "should ignore missing apply method based on a default non-strict apply strategy" do
klass = Class.new do
include AggregateRoot.with_strategy(->{ AggregateRoot::DefaultApplyStrategy.new(strict: false) })
klass = silence_warnings do
Class.new do
include AggregateRoot.with_strategy(->{ AggregateRoot::DefaultApplyStrategy.new(strict: false) })
end
end
order = klass.new
spanish_inquisition = Orders::Events::SpanishInquisition.new
expect { order.apply(spanish_inquisition) }.to_not raise_error
end

it "include with_strategy should warn about depracations" do
expect{
Class.new do
include AggregateRoot.with_strategy(->{ AggregateRoot::DefaultApplyStrategy.new(strict: false) })
end
}.to output(<<~EOW).to_stderr
Please replace include AggregateRoot.with_strategy(...) with include AggregateRoot.with(strategy: ...)
EOW
end

it "include with_default_apply_strategy should warn about depracations" do
expect {
Class.new do
include AggregateRoot.with_default_apply_strategy
end
}.to output(<<~EOW).to_stderr
Please replace include AggregateRoot.with_default_apply_strategy with include AggregateRoot
EOW
end

it "should receive a method call based on a custom strategy" do
strategy = -> do
->(aggregate, event) do
Expand All @@ -53,23 +86,25 @@
}.fetch(event.event_type, ->(ev) {}).call(event)
end
end
klass = Class.new do
include AggregateRoot.with_strategy(strategy)
klass = silence_warnings do
Class.new do
include AggregateRoot.with_strategy(strategy)

def initialize
@status = :draft
end
def initialize
@status = :draft
end

attr_accessor :status
attr_accessor :status

private
private

def custom_created(_event)
@status = :created
end
def custom_created(_event)
@status = :created
end

def custom_expired(_event)
@status = :expired
def custom_expired(_event)
@status = :expired
end
end
end
order = klass.new
Expand Down