Skip to content

Commit 2b4ce59

Browse files
lukaszreszkemostlyobvious
authored andcommitted
Factory method instead of constructor
1 parent f13b20a commit 2b4ce59

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

ruby_event_store/lib/ruby_event_store/projection.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
module RubyEventStore
44
class Projection
5+
private_class_method :new
6+
57
ANONYMOUS_CLASS = "#<Class:".freeze
68

79
def initialize(initial_state = nil)
810
@handlers = {}
911
@init = -> { initial_state }
1012
end
1113

14+
def self.init(initial_state = nil)
15+
new(initial_state)
16+
end
17+
1218
def on(*event_klasses, &block)
1319
raise(ArgumentError, 'No handler block given') unless block_given?
1420

ruby_event_store/spec/projection_spec.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module RubyEventStore
2525

2626
account_balance =
2727
Projection
28-
.new(0)
28+
.init(0)
2929
.on(MoneyDeposited) { |state, event| state += event.data[:amount] }
3030
.on(MoneyWithdrawn) { |state, event| state -= event.data[:amount] }
3131
.call(event_store.read)
@@ -40,7 +40,7 @@ module RubyEventStore
4040

4141
account_balance =
4242
Projection
43-
.new(0)
43+
.init(0)
4444
.on(MoneyDeposited) { |state, event| state += event.data[:amount] }
4545
.on(MoneyWithdrawn) { |state, event| state -= event.data[:amount] }
4646
.call(event_store.read)
@@ -59,7 +59,7 @@ module RubyEventStore
5959
)
6060

6161
stats =
62-
Projection.new({})
62+
Projection.init({})
6363
.on(MoneyDeposited) { |state, event| state[:last_deposit] = event.data[:amount]; state }
6464
.on(MoneyWithdrawn) { |state, event| state[:last_withdrawal] = event.data[:amount]; state }
6565
.call(event_store.read.stream(stream_name))
@@ -74,7 +74,7 @@ module RubyEventStore
7474

7575
deposits =
7676
Projection
77-
.new(0)
77+
.init(0)
7878
.on(MoneyDeposited) { |state, event| state += event.data[:amount] }
7979
.call(event_store.read.stream(stream_name))
8080
expect(deposits).to eq(10)
@@ -88,7 +88,7 @@ module RubyEventStore
8888

8989
cashflow =
9090
Projection
91-
.new(0)
91+
.init(0)
9292
.on(MoneyDeposited, MoneyWithdrawn) { |state, event| state += event.data[:amount] }
9393
.call(event_store.read.stream(stream_name))
9494
expect(cashflow).to eq(12)
@@ -108,7 +108,7 @@ module RubyEventStore
108108

109109
balance =
110110
Projection
111-
.new(0)
111+
.init(0)
112112
.on(MoneyDeposited) { |state, event| state += event.data[:amount] }
113113
.on(MoneyWithdrawn) { |state, event| state -= event.data[:amount] }
114114
.call(event_store.read.stream(stream_name).in_batches(2))
@@ -129,7 +129,7 @@ module RubyEventStore
129129

130130
balance =
131131
Projection
132-
.new(0)
132+
.init(0)
133133
.on(MoneyDeposited) { |state, event| state += event.data[:amount] }
134134
.on(MoneyWithdrawn) { |state, event| state -= event.data[:amount] }
135135
.call(event_store.read.stream(stream_name).from(starting.event_id).in_batches(2))
@@ -145,7 +145,7 @@ module RubyEventStore
145145

146146
balance =
147147
Projection
148-
.new(0)
148+
.init(0)
149149
.on(MoneyDeposited) { |state, event| state += event.data[:amount] }
150150
.on(MoneyWithdrawn) { |state, event| state -= event.data[:amount] }
151151
.call(event_store.read.in_batches(2))
@@ -161,7 +161,7 @@ module RubyEventStore
161161

162162
balance =
163163
Projection
164-
.new(0)
164+
.init(0)
165165
.on(MoneyDeposited) { |state, event| state += event.data[:amount] }
166166
.on(MoneyWithdrawn) { |state, event| state -= event.data[:amount] }
167167
.call(event_store.read.from(starting.event_id).in_batches(2))
@@ -185,7 +185,7 @@ module RubyEventStore
185185

186186
balance =
187187
Projection
188-
.new(0)
188+
.init(0)
189189
.on(MoneyDeposited) { |state, event| state += event.data[:amount] }
190190
.on(MoneyWithdrawn, MoneyLost) { |state, event| state -= event.data[:amount] }
191191
.call(event_store.read.in_batches(100))
@@ -198,39 +198,39 @@ module RubyEventStore
198198
expect(repository).to receive(:read).with(scope.result).and_return([])
199199

200200
Projection
201-
.new(0)
201+
.init(0)
202202
.on(MoneyDeposited) { |state, event| state += event.data[:amount] }
203203
.on(MoneyWithdrawn) { |state, event| state -= event.data[:amount] }
204204
.call(scope)
205205
end
206206

207207
specify "default initial state" do
208-
expect(Projection.new.call([])).to eq(nil)
208+
expect(Projection.init.call([])).to eq(nil)
209209
end
210210

211211
specify "block must be given to on event handlers" do
212212
expect do
213-
Projection.new.on(MoneyDeposited)
213+
Projection.init.on(MoneyDeposited)
214214
end.to raise_error(ArgumentError, "No handler block given")
215215
end
216216

217217
it "does not support anonymous events" do
218218
expect do
219-
Projection.new.on(Class.new) { |_state, _event| }
219+
Projection.init.on(Class.new) { |_state, _event| }
220220
end.to raise_error(ArgumentError, "Anonymous class is missing name")
221221
end
222222

223223
specify do
224224
expect(repository).not_to receive(:read)
225-
state = Projection.new.call(event_store.read)
225+
state = Projection.init.call(event_store.read)
226226
expect(state).to eq(nil)
227227
end
228228

229229
specify do
230230
expect(repository).not_to receive(:read)
231231

232232
initial_state = Object.new
233-
state = Projection.new(initial_state).call(event_store.read)
233+
state = Projection.init(initial_state).call(event_store.read)
234234

235235
expect(state).to eq(initial_state)
236236
end

0 commit comments

Comments
 (0)