ruby_event_store 2.18.0 → 2.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,26 +3,63 @@
3
3
  module RubyEventStore
4
4
  module Mappers
5
5
  class InstrumentedMapper
6
+ DEPRECATION_MESSAGE = <<~EOW
7
+ Instrumentation event names *.rails_event_store are deprecated and will be removed in the next major release.
8
+ Use *.ruby_event_store instead.
9
+ EOW
10
+ private_constant :DEPRECATION_MESSAGE
11
+
12
+ RENAME_DEPRECATION_MESSAGE = <<~EOW
13
+ Instrumentation event names serialize.mapper.ruby_event_store and deserialize.mapper.ruby_event_store are deprecated and will be removed in the next major release.
14
+ Use event_to_record.mapper.ruby_event_store and record_to_event.mapper.ruby_event_store instead.
15
+ The domain_event: payload key in serialize.mapper.ruby_event_store has been renamed to event: in event_to_record.mapper.ruby_event_store.
16
+ EOW
17
+ private_constant :RENAME_DEPRECATION_MESSAGE
18
+
6
19
  def initialize(mapper, instrumentation)
7
20
  @mapper = mapper
8
21
  @instrumentation = instrumentation
9
22
  end
10
23
 
11
24
  def event_to_record(event)
12
- instrumentation.instrument("serialize.mapper.rails_event_store", domain_event: event) do
13
- mapper.event_to_record(event)
25
+ instrumentation.instrument("event_to_record.mapper.ruby_event_store", event: event) do
26
+ deprecated_instrument("serialize.mapper.ruby_event_store", { domain_event: event },
27
+ canonical: "event_to_record.mapper.ruby_event_store",
28
+ message: RENAME_DEPRECATION_MESSAGE) do
29
+ deprecated_instrument("serialize.mapper.rails_event_store", { domain_event: event }) do
30
+ mapper.event_to_record(event)
31
+ end
32
+ end
14
33
  end
15
34
  end
16
35
 
17
36
  def record_to_event(record)
18
- instrumentation.instrument("deserialize.mapper.rails_event_store", record: record) do
19
- mapper.record_to_event(record)
37
+ instrumentation.instrument("record_to_event.mapper.ruby_event_store", record: record) do
38
+ deprecated_instrument("deserialize.mapper.ruby_event_store", { record: record },
39
+ canonical: "record_to_event.mapper.ruby_event_store",
40
+ message: RENAME_DEPRECATION_MESSAGE) do
41
+ deprecated_instrument("deserialize.mapper.rails_event_store", { record: record }) do
42
+ mapper.record_to_event(record)
43
+ end
44
+ end
20
45
  end
21
46
  end
22
47
 
23
48
  private
24
49
 
25
50
  attr_reader :instrumentation, :mapper
51
+
52
+ def deprecated_instrument(name, payload, canonical: nil, message: DEPRECATION_MESSAGE, &block)
53
+ canonical_name = canonical || name.sub("rails_event_store", "ruby_event_store")
54
+ old_listeners = instrumentation.notifier.all_listeners_for(name)
55
+ new_listeners = instrumentation.notifier.all_listeners_for(canonical_name)
56
+ if (old_listeners - new_listeners).any?
57
+ warn message
58
+ instrumentation.instrument(name, payload, &block)
59
+ else
60
+ yield
61
+ end
62
+ end
26
63
  end
27
64
  end
28
65
  end
@@ -4,6 +4,10 @@ module RubyEventStore
4
4
  module Mappers
5
5
  class NullMapper < PipelineMapper
6
6
  def initialize
7
+ warn <<~EOW
8
+ DEPRECATION WARNING: `RubyEventStore::Mappers::NullMapper` is deprecated and will be removed in the next major release.
9
+ Use `RubyEventStore::Mappers::Default.new` instead.
10
+ EOW
7
11
  super(Pipeline.new)
8
12
  end
9
13
  end
@@ -5,6 +5,10 @@ module RubyEventStore
5
5
  module Transformation
6
6
  class EventClassRemapper
7
7
  def initialize(class_map)
8
+ warn <<~EOW
9
+ DEPRECATION WARNING: `RubyEventStore::Mappers::Transformation::EventClassRemapper` is deprecated and will be removed in the next major release.
10
+ Use `RubyEventStore::Mappers::Transformation::Upcast` instead.
11
+ EOW
8
12
  @class_map = class_map
9
13
  end
10
14
 
@@ -2,94 +2,126 @@
2
2
 
3
3
  module RubyEventStore
4
4
  class Projection
5
- private_class_method :new
6
-
7
- def self.from_stream(stream_or_streams)
8
- streams = Array(stream_or_streams)
9
- raise(ArgumentError, "At least one stream must be given") if streams.empty?
10
- new(streams: streams)
5
+ ANONYMOUS_CLASS = "#<Class:".freeze
6
+ DEPRECATION_MESSAGE = <<~EOW
7
+ RubyEventStore::Projection from_stream/from_all_streams/init/when/run API is deprecated and will be removed in the next major release.
8
+ Use Projection.init(initial_state).on(EventClass) { |state, event| new_state }.call(scope) instead.
9
+ EOW
10
+ MULTI_SCOPE_DEPRECATION_MESSAGE = <<~EOW
11
+ Passing multiple scopes to RubyEventStore::Projection#call is deprecated and will be removed in the next major release.
12
+ Use a single scope instead, e.g. call(event_store.read.stream("stream_name")).
13
+ EOW
14
+ NEW_CONSTRUCTOR_DEPRECATION_MESSAGE = <<~EOW
15
+ RubyEventStore::Projection.new is deprecated and will be removed in the next major release.
16
+ Use Projection.init(initial_state) instead.
17
+ EOW
18
+ private_constant :ANONYMOUS_CLASS, :DEPRECATION_MESSAGE, :MULTI_SCOPE_DEPRECATION_MESSAGE, :NEW_CONSTRUCTOR_DEPRECATION_MESSAGE
19
+
20
+ def initialize(initial_state = nil, _internal: false)
21
+ warn NEW_CONSTRUCTOR_DEPRECATION_MESSAGE unless _internal
22
+ @handlers = {}
23
+ @init = -> { initial_state }
24
+ @streams = []
11
25
  end
12
26
 
13
- def self.from_all_streams
14
- new
27
+ def self.init(initial_state = nil)
28
+ new(initial_state, _internal: true)
15
29
  end
16
30
 
17
- def initialize(streams: [])
18
- @streams = streams
19
- @handlers = {}
20
- @init = -> { {} }
21
- end
31
+ def on(*event_klasses, &block)
32
+ raise(ArgumentError, "No handler block given") unless block_given?
22
33
 
23
- attr_reader :streams, :handlers
34
+ event_klasses.each do |event_klass|
35
+ name = event_klass.to_s
36
+ raise(ArgumentError, "Anonymous class is missing name") if name.start_with? ANONYMOUS_CLASS
24
37
 
25
- def init(handler)
26
- @init = handler
38
+ @handlers[name] = ->(state, event) { block.call(state, event) }
39
+ end
27
40
  self
28
41
  end
29
42
 
30
- def when(events, handler)
31
- Array(events).each { |event| handlers[event.to_s] = handler }
43
+ def call(*scopes)
44
+ return initial_state if handled_events.empty?
32
45
 
33
- self
46
+ warn MULTI_SCOPE_DEPRECATION_MESSAGE if scopes.size > 1
47
+
48
+ scopes.reduce(initial_state) do |state, scope|
49
+ scope.of_type(handled_events).reduce(state, &method(:transition))
50
+ end
34
51
  end
35
52
 
36
- def initial_state
37
- @init.call
53
+ def self.from_stream(stream_or_streams)
54
+ warn DEPRECATION_MESSAGE
55
+ streams = Array(stream_or_streams)
56
+ raise(ArgumentError, "At least one stream must be given") if streams.empty?
57
+ projection = new(_internal: true)
58
+ projection.instance_variable_set(:@streams, streams)
59
+ projection
38
60
  end
39
61
 
40
- def current_state
41
- @current_state ||= initial_state
62
+ def self.from_all_streams
63
+ warn DEPRECATION_MESSAGE
64
+ new(_internal: true)
42
65
  end
43
66
 
44
- def call(event)
45
- handlers.fetch(event.event_type).(current_state, event)
67
+ def init(handler)
68
+ warn DEPRECATION_MESSAGE
69
+ @init = handler
70
+ self
46
71
  end
47
72
 
48
- def handled_events
49
- handlers.keys
73
+ def when(events, handler)
74
+ warn DEPRECATION_MESSAGE
75
+ Array(events).each do |event_klass|
76
+ name = event_klass.to_s
77
+ @handlers[name] = ->(state, event) { handler.call(state, event); state }
78
+ end
79
+ self
50
80
  end
51
81
 
52
82
  def run(event_store, start: nil, count: PAGE_SIZE)
53
- return initial_state if handled_events.empty?
54
- streams.any? ? reduce_from_streams(event_store, start, count) : reduce_from_all_streams(event_store, start, count)
83
+ warn DEPRECATION_MESSAGE
84
+
85
+ if @streams.any?
86
+ raise ArgumentError, "Start must be an array with event ids" unless valid_start_for_streams?(start)
87
+ scopes =
88
+ @streams.zip(start || []).map do |stream, start_event_id|
89
+ scope = event_store.read.stream(stream).in_batches(count)
90
+ scope = scope.from(start_event_id) if start_event_id
91
+ scope
92
+ end
93
+ else
94
+ raise ArgumentError, "Start must be valid event id" unless valid_start_for_all_streams?(start)
95
+ scope = event_store.read.in_batches(count)
96
+ scope = scope.from(start) if start
97
+ scopes = [scope]
98
+ end
99
+
100
+ call(*scopes)
55
101
  end
56
102
 
57
103
  private
58
104
 
59
- def valid_starting_point?(start)
60
- return true unless start
61
- streams.any? ? (start.instance_of?(Array) && start.size === streams.size) : start.instance_of?(String)
62
- end
63
-
64
- def reduce_from_streams(event_store, start, count)
65
- raise ArgumentError.new("Start must be an array with event ids") unless valid_starting_point?(start)
66
- streams
67
- .zip(start_events(start))
68
- .reduce(initial_state) do |state, (stream_name, start_event_id)|
69
- read_scope(event_store, stream_name, count, start_event_id).reduce(state, &method(:transition))
70
- end
105
+ def initial_state
106
+ @init.call
71
107
  end
72
108
 
73
- def reduce_from_all_streams(event_store, start, count)
74
- raise ArgumentError.new("Start must be valid event id") unless valid_starting_point?(start)
75
- read_scope(event_store, nil, count, start).reduce(initial_state, &method(:transition))
109
+ def handled_events
110
+ @handlers.keys
76
111
  end
77
112
 
78
- def read_scope(event_store, stream, count, start)
79
- scope = event_store.read.in_batches(count)
80
- scope = scope.of_type(handled_events)
81
- scope = scope.stream(stream) if stream
82
- scope = scope.from(start) if start
83
- scope
113
+ def transition(state, event)
114
+ @handlers.fetch(event.event_type).call(state, event)
84
115
  end
85
116
 
86
- def start_events(start)
87
- start ? start : Array.new
117
+ def valid_start_for_streams?(start)
118
+ return true unless start
119
+ start.instance_of?(Array) && start.size == @streams.size
88
120
  end
89
121
 
90
- def transition(state, event)
91
- handlers.fetch(event.event_type).call(state, event)
92
- state
122
+ def valid_start_for_all_streams?(start)
123
+ return true unless start
124
+ start.instance_of?(String)
93
125
  end
94
126
  end
95
127
  end
@@ -13,7 +13,7 @@ module RubyEventStore
13
13
  end
14
14
 
15
15
  # Limits the query to certain stream.
16
- # {http://railseventstore.org/docs/read/ Find out more}.
16
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
17
17
  #
18
18
  # @param stream_name [String] name of the stream to get events from
19
19
  # @return [Specification]
@@ -22,7 +22,7 @@ module RubyEventStore
22
22
  end
23
23
 
24
24
  # Limits the query to events before or after another event.
25
- # {http://railseventstore.org/docs/read/ Find out more}.
25
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
26
26
  #
27
27
  # @param start [String] id of event to start reading from.
28
28
  # @return [Specification]
@@ -32,7 +32,7 @@ module RubyEventStore
32
32
  end
33
33
 
34
34
  # Limits the query to events before or after another event.
35
- # {http://railseventstore.org/docs/read/ Find out more}.
35
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
36
36
  #
37
37
  # @param stop [String] id of event to start reading from.
38
38
  # @return [Specification]
@@ -42,7 +42,7 @@ module RubyEventStore
42
42
  end
43
43
 
44
44
  # Limits the query to events that occurred before given time.
45
- # {http://railseventstore.org/docs/read/ Find out more}.
45
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
46
46
  #
47
47
  # @param time [Time]
48
48
  # @return [Specification]
@@ -58,7 +58,7 @@ module RubyEventStore
58
58
  end
59
59
 
60
60
  # Limits the query to events that occurred on or before given time.
61
- # {http://railseventstore.org/docs/read/ Find out more}.
61
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
62
62
  #
63
63
  # @param time [Time]
64
64
  # @return [Specification]
@@ -74,7 +74,7 @@ module RubyEventStore
74
74
  end
75
75
 
76
76
  # Limits the query to events that occurred after given time.
77
- # {http://railseventstore.org/docs/read/ Find out more}.
77
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
78
78
  #
79
79
  # @param time [Time]
80
80
  # @return [Specification]
@@ -90,7 +90,7 @@ module RubyEventStore
90
90
  end
91
91
 
92
92
  # Limits the query to events that occurred on or after given time.
93
- # {http://railseventstore.org/docs/read/ Find out more}.
93
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
94
94
  #
95
95
  # @param time [Time]
96
96
  # @return [Specification]
@@ -106,7 +106,7 @@ module RubyEventStore
106
106
  end
107
107
 
108
108
  # Limits the query to events within given time range.
109
- # {http://railseventstore.org/docs/read/ Find out more}.
109
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
110
110
  #
111
111
  # @param time_range [Range]
112
112
  # @return [Specification]
@@ -119,7 +119,7 @@ module RubyEventStore
119
119
  end
120
120
 
121
121
  # Sets the order of time sorting using transaction time
122
- # {http://railseventstore.org/docs/read/ Find out more}
122
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}
123
123
  #
124
124
  # @return [Specification]
125
125
  def as_at
@@ -127,7 +127,7 @@ module RubyEventStore
127
127
  end
128
128
 
129
129
  # Sets the order of time sorting using validity time
130
- # {http://railseventstore.org/docs/read/ Find out more}
130
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}
131
131
  #
132
132
  # @return [Specification]
133
133
  def as_of
@@ -135,7 +135,7 @@ module RubyEventStore
135
135
  end
136
136
 
137
137
  # Sets the order of reading events to ascending (forward from the start).
138
- # {http://railseventstore.org/docs/read/ Find out more}.
138
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
139
139
  #
140
140
  # @return [Specification]
141
141
  def forward
@@ -143,7 +143,7 @@ module RubyEventStore
143
143
  end
144
144
 
145
145
  # Sets the order of reading events to descending (backward from the start).
146
- # {http://railseventstore.org/docs/read/ Find out more}.
146
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
147
147
  #
148
148
  # @return [Specification]
149
149
  def backward
@@ -151,7 +151,7 @@ module RubyEventStore
151
151
  end
152
152
 
153
153
  # Limits the query to specified number of events.
154
- # {http://railseventstore.org/docs/read/ Find out more}.
154
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
155
155
  #
156
156
  # @param count [Integer] maximal number of events to retrieve
157
157
  # @return [Specification]
@@ -162,7 +162,7 @@ module RubyEventStore
162
162
 
163
163
  # Executes the query based on the specification built up to this point.
164
164
  # Yields each batch of records that was retrieved from the store.
165
- # {http://railseventstore.org/docs/read/ Find out more}.
165
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
166
166
  #
167
167
  # @yield [Array<Event>] batch of events
168
168
  # @return [Enumerator, nil] Enumerator is returned when block not given
@@ -174,7 +174,7 @@ module RubyEventStore
174
174
 
175
175
  # Executes the query based on the specification built up to this point.
176
176
  # Yields events read from the store if block given. Otherwise, returns enumerable collection.
177
- # {http://railseventstore.org/docs/read/ Find out more}.
177
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
178
178
  #
179
179
  # @yield [Event] event
180
180
  # @return [Enumerator, nil] Enumerator is returned when block not given
@@ -186,7 +186,7 @@ module RubyEventStore
186
186
 
187
187
  # Executes the query based on the specification built up to this point
188
188
  # and maps the result using provided block.
189
- # {http://railseventstore.org/docs/read/ Find out more}.
189
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
190
190
  #
191
191
  # @return [Array] of mapped result
192
192
  def map(&block)
@@ -196,7 +196,7 @@ module RubyEventStore
196
196
 
197
197
  # Reduces the results of the query based on the specification
198
198
  # built up to this point result using provided block.
199
- # {http://railseventstore.org/docs/read/ Find out more}.
199
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
200
200
  #
201
201
  # @param accumulator starting state for reduce operation
202
202
  # @return reduce result as defined by block given
@@ -206,7 +206,7 @@ module RubyEventStore
206
206
  end
207
207
 
208
208
  # Calculates the size of result set based on the specification build up to this point.
209
- # {http://railseventstore.org/docs/read/ Find out more}.
209
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
210
210
  #
211
211
  # @return [Integer] Number of events to read
212
212
  def count
@@ -215,7 +215,7 @@ module RubyEventStore
215
215
 
216
216
  # Executes the query based on the specification built up to this point.
217
217
  # Returns array of domain events.
218
- # {http://railseventstore.org/docs/read/ Find out more}.
218
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
219
219
  #
220
220
  # @return [Array<Event>]
221
221
  def to_a
@@ -223,7 +223,7 @@ module RubyEventStore
223
223
  end
224
224
 
225
225
  # Specifies that events should be obtained in batches.
226
- # {http://railseventstore.org/docs/read/ Find out more}.
226
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
227
227
  #
228
228
  # Looping through a collection of events from the store
229
229
  # can be inefficient since it will try to instantiate all
@@ -244,10 +244,18 @@ module RubyEventStore
244
244
  end,
245
245
  )
246
246
  end
247
- alias in_batches_of in_batches
247
+
248
+ def in_batches_of(batch_size = DEFAULT_BATCH_SIZE)
249
+ warn <<~EOW
250
+ RubyEventStore::Specification#in_batches_of is deprecated and will be removed in the next major release.
251
+
252
+ Use #in_batches instead.
253
+ EOW
254
+ in_batches(batch_size)
255
+ end
248
256
 
249
257
  # Specifies that only first event should be read.
250
- # {http://railseventstore.org/docs/read/ Find out more}.
258
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
251
259
  #
252
260
  # @return [Specification]
253
261
  def read_first
@@ -255,7 +263,7 @@ module RubyEventStore
255
263
  end
256
264
 
257
265
  # Specifies that only last event should be read.
258
- # {http://railseventstore.org/docs/read/ Find out more}.
266
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
259
267
  #
260
268
  # @return [Specification]
261
269
  def read_last
@@ -264,7 +272,7 @@ module RubyEventStore
264
272
 
265
273
  # Executes the query based on the specification built up to this point.
266
274
  # Returns the first event in specified collection of events.
267
- # {http://railseventstore.org/docs/read/ Find out more}.
275
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
268
276
  #
269
277
  # @return [Event, nil]
270
278
  def first
@@ -273,7 +281,7 @@ module RubyEventStore
273
281
 
274
282
  # Executes the query based on the specification built up to this point.
275
283
  # Returns the last event in specified collection of events.
276
- # {http://railseventstore.org/docs/read/ Find out more}.
284
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
277
285
  #
278
286
  # @return [Event, nil]
279
287
  def last
@@ -281,17 +289,25 @@ module RubyEventStore
281
289
  end
282
290
 
283
291
  # Limits the query to certain event type(s).
284
- # {http://railseventstore.org/docs/read/ Find out more}.
292
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
285
293
  #
286
294
  # @types [Class, Array(Class)] types of event to look for.
287
295
  # @return [Specification]
288
296
  def of_type(*types)
289
297
  Specification.new(reader, result.dup { |r| r.with_types = types.flatten })
290
298
  end
291
- alias_method :of_types, :of_type
299
+
300
+ def of_types(*types)
301
+ warn <<~EOW
302
+ RubyEventStore::Specification#of_types is deprecated and will be removed in the next major release.
303
+
304
+ Use #of_type instead.
305
+ EOW
306
+ of_type(*types)
307
+ end
292
308
 
293
309
  # Limits the query to certain events by given even ids.
294
- # {http://railseventstore.org/docs/read/ Find out more}.
310
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
295
311
  #
296
312
  # @param event_ids [Array(String)] ids of event to look for.
297
313
  # @return [Specification]
@@ -302,7 +318,7 @@ module RubyEventStore
302
318
  # Reads single event from repository.
303
319
  # Returns the event with specified id or nil if event is not found
304
320
  # in specified collection of events.
305
- # {http://railseventstore.org/docs/read/ Find out more}.
321
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
306
322
  #
307
323
  # @return [Event, nil]
308
324
  def event(event_id)
@@ -312,7 +328,7 @@ module RubyEventStore
312
328
  # Reads single existing event from repository.
313
329
  # Returns the event with specified id or raises [EventNotFound] error if
314
330
  # event is not found in specified collection of events.
315
- # {http://railseventstore.org/docs/read/ Find out more}.
331
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
316
332
  #
317
333
  # @return [Event]
318
334
  def event!(event_id)
@@ -322,7 +338,7 @@ module RubyEventStore
322
338
  # Reads all events of given ids from repository.
323
339
  # Yields each event (found by id in specified collection of events)
324
340
  # read from the store if block given. Otherwise, returns enumerable collection.
325
- # {http://railseventstore.org/docs/read/ Find out more}.
341
+ # {https://railseventstore.org/docs/core-concepts/read Find out more}.
326
342
  #
327
343
  # @yield [Event] event
328
344
  # @return [Enumerator] Enumerator is returned when block not given