ruby_event_store 2.9.1 → 2.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 034aa1fb571c22b4e87df0161a00192535fdf8d6637df0e3db2fa9dc14c928d6
4
- data.tar.gz: bbb8846d15f41eadf5e0c4cb3d63697ed80c536cfbd2c921453038ec40d0b3f9
3
+ metadata.gz: 20f2bfe7214045324bd5fbe76b4e279b79f7b4a363fc3096fa0c940f6b661a9d
4
+ data.tar.gz: 1d80b75cdc741164a713a27f0650ade6d53d35b64c3622877c7dff1b131856ae
5
5
  SHA512:
6
- metadata.gz: 5deb5169c06c20dfb077a0972a262fbf43fb1bf1e27257dd756a22d6f42eb34d1b486f34cebf7d55579bddb271b729ccd5e377d9f30ca5ed0abe814bba046ea5
7
- data.tar.gz: 5188078974d0eb3d2311d00de74e69e5756e5d27b9afd7d1dc0cbbdc7b164fb26e6c27f52025215e2cd4978dc11e9eaa41f92f70502bd5516439a2205d8166b9
6
+ metadata.gz: 648428aaada1d0bb4e088699fe5360db5546f9809eab9e3e0a7527a3475e563b8066d1cadd90c2ecbee2e7a2af4db32925877b5a5baa9990233f00fa3ed9ae7f
7
+ data.tar.gz: 64635cdbbdc428f7ce43e3339fa0a1f45df31ace522753702ba5d6cc955181b9f664ec318b78578b14360a18f1a4393ca7e2c512241247ea85600657dc3e0318
@@ -10,7 +10,8 @@ module RubyEventStore
10
10
  subscriptions: Subscriptions.new,
11
11
  dispatcher: Dispatcher.new,
12
12
  clock: default_clock,
13
- correlation_id_generator: default_correlation_id_generator
13
+ correlation_id_generator: default_correlation_id_generator,
14
+ event_type_resolver: EventTypeResolver.new
14
15
  )
15
16
  @repository = repository
16
17
  @mapper = mapper
@@ -19,6 +20,7 @@ module RubyEventStore
19
20
  @clock = clock
20
21
  @metadata = Concurrent::ThreadLocalVar.new
21
22
  @correlation_id_generator = correlation_id_generator
23
+ @event_type_resolver = event_type_resolver
22
24
  end
23
25
 
24
26
  # Persists events and notifies subscribed handlers about them
@@ -142,7 +144,7 @@ module RubyEventStore
142
144
  def subscribe(subscriber = nil, to:, &proc)
143
145
  raise ArgumentError, "subscriber must be first argument or block, cannot be both" if subscriber && proc
144
146
  subscriber ||= proc
145
- broker.add_subscription(subscriber, to)
147
+ broker.add_subscription(subscriber, to.map { |event_klass| event_type_resolver.call(event_klass) })
146
148
  end
147
149
 
148
150
  # Subscribes a handler (subscriber) that will be invoked for all published events
@@ -172,11 +174,12 @@ module RubyEventStore
172
174
  # which are active only during the invocation of the provided
173
175
  # block of code.
174
176
  class Within
175
- def initialize(block, broker)
177
+ def initialize(block, broker, resolver)
176
178
  @block = block
177
179
  @broker = broker
178
180
  @global_subscribers = []
179
181
  @subscribers = Hash.new { [] }
182
+ @resolver = resolver
180
183
  end
181
184
 
182
185
  # Subscribes temporary handlers that
@@ -210,7 +213,7 @@ module RubyEventStore
210
213
  # @return [self]
211
214
  def subscribe(handler = nil, to:, &handler2)
212
215
  raise ArgumentError if handler && handler2
213
- @subscribers[handler || handler2] += Array(to)
216
+ @subscribers[handler || handler2] += Array(to).map { |event_klass| resolver.call(event_klass) }
214
217
  self
215
218
  end
216
219
 
@@ -228,6 +231,7 @@ module RubyEventStore
228
231
  end
229
232
 
230
233
  private
234
+ attr_reader :resolver
231
235
 
232
236
  def add_thread_subscribers
233
237
  @subscribers.map { |subscriber, types| @broker.add_thread_subscription(subscriber, types) }
@@ -245,7 +249,7 @@ module RubyEventStore
245
249
  # @return [Within] builder object which collects temporary subscriptions
246
250
  def within(&block)
247
251
  raise ArgumentError if block.nil?
248
- Within.new(block, broker)
252
+ Within.new(block, broker, event_type_resolver)
249
253
  end
250
254
 
251
255
  # Set additional metadata for all events published within the provided block
@@ -357,10 +361,6 @@ module RubyEventStore
357
361
 
358
362
  protected
359
363
 
360
- def event_type_resolver
361
- subscriptions.event_type_resolver
362
- end
363
-
364
364
  def metadata=(value)
365
365
  @metadata.value = value
366
366
  end
@@ -373,6 +373,6 @@ module RubyEventStore
373
373
  -> { SecureRandom.uuid }
374
374
  end
375
375
 
376
- attr_reader :repository, :mapper, :subscriptions, :broker, :clock, :correlation_id_generator
376
+ attr_reader :repository, :mapper, :subscriptions, :broker, :clock, :correlation_id_generator, :event_type_resolver
377
377
  end
378
378
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyEventStore
4
+ class EventTypeResolver
5
+ def call(value)
6
+ value.to_s
7
+ end
8
+ end
9
+ end
@@ -69,7 +69,7 @@ module RubyEventStore
69
69
  read_events(repository, specification.backward, stream, from: from, to: to, count: count)
70
70
  end
71
71
 
72
- it "just created is empty" do
72
+ specify "just created is empty" do
73
73
  expect(read_events_forward(repository)).to be_empty
74
74
  end
75
75
 
@@ -22,8 +22,8 @@ RSpec.shared_examples :subscriptions do |subscriptions_class|
22
22
  another_handler = TestHandler.new
23
23
  global_handler = TestHandler.new
24
24
 
25
- subscriptions.add_subscription(handler, [Test1DomainEvent, Test3DomainEvent])
26
- subscriptions.add_subscription(another_handler, [Test2DomainEvent])
25
+ subscriptions.add_subscription(handler, ['Test1DomainEvent', 'Test3DomainEvent'])
26
+ subscriptions.add_subscription(another_handler, ['Test2DomainEvent'])
27
27
  subscriptions.add_global_subscription(global_handler)
28
28
 
29
29
  expect(subscriptions.all_for("Test1DomainEvent")).to eq([handler, global_handler])
@@ -36,15 +36,14 @@ RSpec.shared_examples :subscriptions do |subscriptions_class|
36
36
  another_handler = TestHandler.new
37
37
  global_handler = TestHandler.new
38
38
 
39
- subscriptions.add_thread_subscription(handler, [Test1DomainEvent, Test3DomainEvent])
40
- subscriptions.add_thread_subscription(another_handler, [Test2DomainEvent])
39
+ subscriptions.add_thread_subscription(handler, ['Test1DomainEvent', 'Test3DomainEvent'])
40
+ subscriptions.add_thread_subscription(another_handler, ['Test2DomainEvent'])
41
41
  subscriptions.add_thread_global_subscription(global_handler)
42
- t =
43
- Thread.new do
44
- subscriptions.add_thread_subscription(handler, [Test2DomainEvent])
45
- subscriptions.add_thread_global_subscription(another_handler)
46
- expect(subscriptions.all_for("Test2DomainEvent")).to eq([another_handler, handler])
47
- end
42
+ t = Thread.new do
43
+ subscriptions.add_thread_subscription(handler, ['Test2DomainEvent'])
44
+ subscriptions.add_thread_global_subscription(another_handler)
45
+ expect(subscriptions.all_for('Test2DomainEvent')).to eq([another_handler, handler])
46
+ end
48
47
 
49
48
  expect(subscriptions.all_for("Test1DomainEvent")).to eq([global_handler, handler])
50
49
  expect(subscriptions.all_for("Test2DomainEvent")).to eq([global_handler, another_handler])
@@ -78,9 +77,9 @@ RSpec.shared_examples :subscriptions do |subscriptions_class|
78
77
  it "revokes subscription" do
79
78
  handler = TestHandler.new
80
79
 
81
- revoke = subscriptions.add_subscription(handler, [Test1DomainEvent, Test2DomainEvent])
82
- expect(subscriptions.all_for("Test1DomainEvent")).to eq([handler])
83
- expect(subscriptions.all_for("Test2DomainEvent")).to eq([handler])
80
+ revoke = subscriptions.add_subscription(handler, ['Test1DomainEvent', 'Test2DomainEvent'])
81
+ expect(subscriptions.all_for('Test1DomainEvent')).to eq([handler])
82
+ expect(subscriptions.all_for('Test2DomainEvent')).to eq([handler])
84
83
  revoke.()
85
84
  expect(subscriptions.all_for("Test1DomainEvent")).to eq([])
86
85
  expect(subscriptions.all_for("Test2DomainEvent")).to eq([])
@@ -100,9 +99,9 @@ RSpec.shared_examples :subscriptions do |subscriptions_class|
100
99
  it "revokes thread subscription" do
101
100
  handler = TestHandler.new
102
101
 
103
- revoke = subscriptions.add_thread_subscription(handler, [Test1DomainEvent, Test2DomainEvent])
104
- expect(subscriptions.all_for("Test1DomainEvent")).to eq([handler])
105
- expect(subscriptions.all_for("Test2DomainEvent")).to eq([handler])
102
+ revoke = subscriptions.add_thread_subscription(handler, ['Test1DomainEvent', 'Test2DomainEvent'])
103
+ expect(subscriptions.all_for('Test1DomainEvent')).to eq([handler])
104
+ expect(subscriptions.all_for('Test2DomainEvent')).to eq([handler])
106
105
  revoke.()
107
106
  expect(subscriptions.all_for("Test1DomainEvent")).to eq([])
108
107
  expect(subscriptions.all_for("Test2DomainEvent")).to eq([])
@@ -116,10 +115,10 @@ RSpec.shared_examples :subscriptions do |subscriptions_class|
116
115
  expect(subscriptions.all_for("Test1DomainEvent")).to eq([handler, handler])
117
116
  end
118
117
 
119
- it "subscribes by type of event which is a class" do
118
+ it 'subscribes by type of event which is a class' do
120
119
  handler = TestHandler.new
121
- subscriptions.add_subscription(handler, [Test1DomainEvent])
122
- subscriptions.add_thread_subscription(handler, [Test1DomainEvent])
120
+ subscriptions.add_subscription(handler, ['Test1DomainEvent'])
121
+ subscriptions.add_thread_subscription(handler, ['Test1DomainEvent'])
123
122
 
124
123
  expect(subscriptions.all_for("Test1DomainEvent")).to eq([handler, handler])
125
124
  end
@@ -4,15 +4,14 @@ require "concurrent"
4
4
 
5
5
  module RubyEventStore
6
6
  class Subscriptions
7
- def initialize(event_type_resolver: default_event_type_resolver)
8
- @event_type_resolver = event_type_resolver
7
+ def initialize
9
8
  @local = LocalSubscriptions.new
10
9
  @global = GlobalSubscriptions.new
11
10
  @thread = ThreadSubscriptions.new
12
11
  end
13
12
 
14
13
  def add_subscription(subscriber, event_types)
15
- local.add(subscriber, resolve_event_types(event_types))
14
+ local.add(subscriber, event_types)
16
15
  end
17
16
 
18
17
  def add_global_subscription(subscriber)
@@ -20,7 +19,7 @@ module RubyEventStore
20
19
  end
21
20
 
22
21
  def add_thread_subscription(subscriber, event_types)
23
- thread.local.add(subscriber, resolve_event_types(event_types))
22
+ thread.local.add(subscriber, event_types)
24
23
  end
25
24
 
26
25
  def add_thread_global_subscription(subscriber)
@@ -31,24 +30,10 @@ module RubyEventStore
31
30
  [local, global, thread].map { |r| r.all_for(event_type) }.reduce(&:+)
32
31
  end
33
32
 
34
- attr_reader :event_type_resolver
35
-
36
33
  private
37
34
 
38
35
  attr_reader :local, :global, :thread
39
36
 
40
- def default_event_type_resolver
41
- ->(value) { value.to_s }
42
- end
43
-
44
- def resolve_event_types(event_types)
45
- event_types.map(&method(:resolve_event_type))
46
- end
47
-
48
- def resolve_event_type(type)
49
- event_type_resolver.call(type)
50
- end
51
-
52
37
  class ThreadSubscriptions
53
38
  def initialize
54
39
  @local = ThreadLocalSubscriptions.new
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyEventStore
4
- VERSION = "2.9.1"
4
+ VERSION = "2.10"
5
5
  end
@@ -46,3 +46,4 @@ require_relative "ruby_event_store/version"
46
46
  require_relative "ruby_event_store/instrumented_repository"
47
47
  require_relative "ruby_event_store/instrumented_dispatcher"
48
48
  require_relative "ruby_event_store/instrumented_subscriptions"
49
+ require_relative "ruby_event_store/event_type_resolver"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_event_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.1
4
+ version: '2.10'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arkency
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-03 00:00:00.000000000 Z
11
+ date: 2023-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -50,6 +50,7 @@ files:
50
50
  - lib/ruby_event_store/dispatcher.rb
51
51
  - lib/ruby_event_store/errors.rb
52
52
  - lib/ruby_event_store/event.rb
53
+ - lib/ruby_event_store/event_type_resolver.rb
53
54
  - lib/ruby_event_store/expected_version.rb
54
55
  - lib/ruby_event_store/immediate_async_dispatcher.rb
55
56
  - lib/ruby_event_store/in_memory_repository.rb
@@ -118,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
119
  - !ruby/object:Gem::Version
119
120
  version: '0'
120
121
  requirements: []
121
- rubygems_version: 3.3.7
122
+ rubygems_version: 3.4.8
122
123
  signing_key:
123
124
  specification_version: 4
124
125
  summary: Implementation of an event store in Ruby