Skip to content

Commit 1193273

Browse files
mpraglowskimostlyobvious
authored andcommitted
Add deprecation warning for with_strategu & with_default_apply_strategu includes
1 parent 42b2dcc commit 1193273

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

aggregate_root/lib/aggregate_root.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,18 @@ def unpublished_events
6565
def self.with_default_apply_strategy
6666
Module.new do
6767
def self.included(host_class)
68-
host_class.extend OnDSL
69-
host_class.include AggregateRoot.with
68+
warn <<~EOW
69+
Please replace include AggregateRoot.with_default_apply_strategy with include AggregateRoot
70+
EOW
71+
host_class.include AggregateRoot
7072
end
7173
end
7274
end
7375

7476
def self.with_strategy(strategy)
77+
warn <<~EOW
78+
Please replace include AggregateRoot.with_strategy(...) with include AggregateRoot.with(strategy: ...)
79+
EOW
7580
with(strategy: strategy)
7681
end
7782

@@ -89,6 +94,7 @@ def self.included(host_class)
8994
end
9095

9196
def self.included(host_class)
92-
host_class.include with_default_apply_strategy
97+
host_class.extend OnDSL
98+
host_class.include with
9399
end
94100
end

aggregate_root/spec/aggregate_root_spec.rb

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,48 @@
3535
expect { order.apply(spanish_inquisition) }.to raise_error(AggregateRoot::MissingHandler, "Missing handler method apply_spanish_inquisition on aggregate Order")
3636
end
3737

38+
it "should raise error for missing apply method based on a default apply strategy (explicity stated)" do
39+
klass = silence_warnings do
40+
Class.new do
41+
include AggregateRoot.with_default_apply_strategy
42+
end
43+
end
44+
order = klass.new
45+
spanish_inquisition = Orders::Events::SpanishInquisition.new
46+
expect { order.apply(spanish_inquisition) }.to raise_error(AggregateRoot::MissingHandler, "Missing handler method apply_spanish_inquisition on aggregate #{klass}")
47+
end
48+
3849
it "should ignore missing apply method based on a default non-strict apply strategy" do
39-
klass = Class.new do
40-
include AggregateRoot.with_strategy(->{ AggregateRoot::DefaultApplyStrategy.new(strict: false) })
50+
klass = silence_warnings do
51+
Class.new do
52+
include AggregateRoot.with_strategy(->{ AggregateRoot::DefaultApplyStrategy.new(strict: false) })
53+
end
4154
end
4255
order = klass.new
4356
spanish_inquisition = Orders::Events::SpanishInquisition.new
4457
expect { order.apply(spanish_inquisition) }.to_not raise_error
4558
end
4659

60+
it "include with_strategy should warn about depracations" do
61+
expect{
62+
Class.new do
63+
include AggregateRoot.with_strategy(->{ AggregateRoot::DefaultApplyStrategy.new(strict: false) })
64+
end
65+
}.to output(<<~EOW).to_stderr
66+
Please replace include AggregateRoot.with_strategy(...) with include AggregateRoot.with(strategy: ...)
67+
EOW
68+
end
69+
70+
it "include with_default_apply_strategy should warn about depracations" do
71+
expect {
72+
Class.new do
73+
include AggregateRoot.with_default_apply_strategy
74+
end
75+
}.to output(<<~EOW).to_stderr
76+
Please replace include AggregateRoot.with_default_apply_strategy with include AggregateRoot
77+
EOW
78+
end
79+
4780
it "should receive a method call based on a custom strategy" do
4881
strategy = -> do
4982
->(aggregate, event) do
@@ -53,23 +86,25 @@
5386
}.fetch(event.event_type, ->(ev) {}).call(event)
5487
end
5588
end
56-
klass = Class.new do
57-
include AggregateRoot.with_strategy(strategy)
89+
klass = silence_warnings do
90+
Class.new do
91+
include AggregateRoot.with_strategy(strategy)
5892

59-
def initialize
60-
@status = :draft
61-
end
93+
def initialize
94+
@status = :draft
95+
end
6296

63-
attr_accessor :status
97+
attr_accessor :status
6498

65-
private
99+
private
66100

67-
def custom_created(_event)
68-
@status = :created
69-
end
101+
def custom_created(_event)
102+
@status = :created
103+
end
70104

71-
def custom_expired(_event)
72-
@status = :expired
105+
def custom_expired(_event)
106+
@status = :expired
107+
end
73108
end
74109
end
75110
order = klass.new

0 commit comments

Comments
 (0)