diff --git a/aggregate_root/lib/aggregate_root.rb b/aggregate_root/lib/aggregate_root.rb index 0124909dd7..a83f3de066 100644 --- a/aggregate_root/lib/aggregate_root.rb +++ b/aggregate_root/lib/aggregate_root.rb @@ -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}" @@ -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 @@ -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 end end diff --git a/aggregate_root/spec/aggregate_root_spec.rb b/aggregate_root/spec/aggregate_root_spec.rb index 4acf09ebad..3233d08c6c 100644 --- a/aggregate_root/spec/aggregate_root_spec.rb +++ b/aggregate_root/spec/aggregate_root_spec.rb @@ -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 @@ -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