ruby_event_store 0.6.0 → 0.7.0

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
  SHA1:
3
- metadata.gz: 7ae9a071c07547c47d1e9f3ecfdfdc451e9ce235
4
- data.tar.gz: 9271a2095a44bceedbe8b4908a6deaff0be50d19
3
+ metadata.gz: 2cd828fa90780ea61710d291386fb028b8357653
4
+ data.tar.gz: e6db6f0131ce5c157cacbdad680526fe1455438e
5
5
  SHA512:
6
- metadata.gz: a86709c96b0819bbfccff72ae50d27c9813e0bdc7a30a48a6c5a1c41613a3e2aec81cd8def32d96c89e29147ed6d0e5087d664e98a2fcde1fd7b3783664a25a2
7
- data.tar.gz: 77c426dfe5eb9e7314f4a86f3d15cddae2acf5b05064427eea7e64fc343cf27532a9d439a2ffd0a5546a5597c67be64a75eba9ecdb74fb249263eb8c12d5c83c
6
+ metadata.gz: fdf4b6b333d7143de08b36948f84e3bfe9af52aecac47bd6de41470158dcdd126f5497aa2cb6707fb8382cb1836894b68914007870ffad0c37abb64d562d176e
7
+ data.tar.gz: 6e3fb60f508e6a521edbad7d14414b5351b3d25a42ea34c0150af3ba268371668fb6625017a4e31cddebbf49d6bd88699f1dddbe6d4c0292dec4e0fac7a31c3e
@@ -1,4 +1,7 @@
1
- ### HEAD
1
+ ### 0.7.0 (21.06.2016)
2
+
3
+ * Change: support for Dynamic subscriptions PR #20
4
+ * Change: Add lint for 3rd party implementations of event broker PR #21
2
5
 
3
6
  ### 0.6.0 (25.05.2016)
4
7
 
@@ -56,15 +56,25 @@ module RubyEventStore
56
56
  repository.read_all_streams_backward(page.start, page.count)
57
57
  end
58
58
 
59
- def subscribe(subscriber, event_types)
60
- event_broker.add_subscriber(subscriber, event_types)
59
+ def subscribe(subscriber, event_types, &proc)
60
+ event_broker.add_subscriber(subscriber, event_types).tap do |unsub|
61
+ handle_subscribe(unsub, &proc)
62
+ end
61
63
  end
62
64
 
63
- def subscribe_to_all_events(subscriber)
64
- event_broker.add_global_subscriber(subscriber)
65
+ def subscribe_to_all_events(subscriber, &proc)
66
+ event_broker.add_global_subscriber(subscriber).tap do |unsub|
67
+ handle_subscribe(unsub, &proc)
68
+ end
65
69
  end
66
70
 
67
71
  private
72
+ def handle_subscribe(unsub)
73
+ if block_given?
74
+ yield
75
+ unsub.()
76
+ end
77
+ end
68
78
 
69
79
  class Page
70
80
  def initialize(repository, start, count)
@@ -17,6 +17,8 @@ module RubyEventStore
17
17
  def add_global_subscriber(subscriber)
18
18
  verify_subscriber(subscriber)
19
19
  @global_subscribers << subscriber
20
+
21
+ ->() { @global_subscribers.delete(subscriber) }
20
22
  end
21
23
 
22
24
  def notify_subscribers(event)
@@ -34,9 +36,8 @@ module RubyEventStore
34
36
  end
35
37
 
36
38
  def subscribe(subscriber, event_types)
37
- event_types.each do |type|
38
- subscribers[type] << subscriber
39
- end
39
+ event_types.each{ |type| subscribers[type] << subscriber }
40
+ ->() {event_types.each{ |type| subscribers[type].delete(subscriber) } }
40
41
  end
41
42
 
42
43
  def ensure_method_defined(subscriber)
@@ -0,0 +1,117 @@
1
+ RSpec.shared_examples :event_broker do |broker_class|
2
+ Test1DomainEvent = Class.new(RubyEventStore::Event)
3
+ Test2DomainEvent = Class.new(RubyEventStore::Event)
4
+ Test3DomainEvent = Class.new(RubyEventStore::Event)
5
+
6
+ class IncorrectDenormalizer
7
+ end
8
+ class TestHandler
9
+ def initialize
10
+ @events = []
11
+ end
12
+
13
+ def handle_event(event)
14
+ @events << event
15
+ end
16
+
17
+ attr_reader :events
18
+ end
19
+ class TestDispatcher
20
+ attr_reader :dispatched
21
+
22
+ def initialize
23
+ @dispatched = []
24
+ end
25
+
26
+ def call(subscriber, event)
27
+ @dispatched << {subscriber: subscriber, event: event}
28
+ end
29
+ end
30
+
31
+ subject(:broker) { broker_class.new }
32
+
33
+ it 'raise error when no subscriber' do
34
+ expect { broker.add_subscriber(nil, [])}.to raise_error(RubyEventStore::SubscriberNotExist)
35
+ expect { broker.add_global_subscriber(nil)}.to raise_error(RubyEventStore::SubscriberNotExist)
36
+ end
37
+
38
+ it 'notify subscribed handlers' do
39
+ handler = TestHandler.new
40
+ another_handler = TestHandler.new
41
+ global_handler = TestHandler.new
42
+
43
+ broker.add_subscriber(handler, [Test1DomainEvent, Test3DomainEvent])
44
+ broker.add_subscriber(another_handler, [Test2DomainEvent])
45
+ broker.add_global_subscriber(global_handler)
46
+
47
+ event1 = Test1DomainEvent.new
48
+ event2 = Test2DomainEvent.new
49
+ event3 = Test3DomainEvent.new
50
+
51
+ [event1, event2, event3].each do |ev|
52
+ broker.notify_subscribers(ev)
53
+ end
54
+
55
+ expect(handler.events).to eq([event1,event3])
56
+ expect(another_handler.events).to eq([event2])
57
+ expect(global_handler.events).to eq([event1,event2,event3])
58
+ end
59
+
60
+ it 'raises error when no valid method on handler' do
61
+ message = "#handle_event method is not found " +
62
+ "in Subscribers::IncorrectDenormalizer subscriber." +
63
+ " Are you sure it is a valid subscriber?"
64
+ subscriber = Subscribers::IncorrectDenormalizer.new
65
+ expect { broker.add_subscriber(subscriber, [])}.to raise_error(RubyEventStore::MethodNotDefined, message)
66
+ expect { broker.add_global_subscriber(subscriber)}.to raise_error(RubyEventStore::MethodNotDefined, message)
67
+ end
68
+
69
+ it 'returns lambda as an output of global subscribe methods' do
70
+ handler = TestHandler.new
71
+ result = broker.add_global_subscriber(handler)
72
+ expect(result).to respond_to(:call)
73
+ end
74
+
75
+ it 'return lambda as an output of subscribe methods' do
76
+ handler = TestHandler.new
77
+ result = broker.add_subscriber(handler, [Test1DomainEvent, Test2DomainEvent])
78
+ expect(result).to respond_to(:call)
79
+ end
80
+
81
+ it 'revokes global subscription' do
82
+ handler = TestHandler.new
83
+ event1 = Test1DomainEvent.new
84
+ event2 = Test2DomainEvent.new
85
+
86
+ revoke = broker.add_global_subscriber(handler)
87
+ broker.notify_subscribers(event1)
88
+ expect(handler.events).to eq([event1])
89
+ revoke.()
90
+ broker.notify_subscribers(event2)
91
+ expect(handler.events).to eq([event1])
92
+ end
93
+
94
+ it 'revokes subscription' do
95
+ handler = TestHandler.new
96
+ event1 = Test1DomainEvent.new
97
+ event2 = Test2DomainEvent.new
98
+
99
+ revoke = broker.add_subscriber(handler, [Test1DomainEvent, Test2DomainEvent])
100
+ broker.notify_subscribers(event1)
101
+ expect(handler.events).to eq([event1])
102
+ revoke.()
103
+ broker.notify_subscribers(event2)
104
+ expect(handler.events).to eq([event1])
105
+ end
106
+
107
+ it 'allows to provide a custom dispatcher' do
108
+ dispatcher = TestDispatcher.new
109
+ handler = TestHandler.new
110
+ event1 = Test1DomainEvent.new
111
+ broker_with_custom_dispatcher = broker_class.new(dispatcher)
112
+ broker_with_custom_dispatcher.add_subscriber(handler, [Test1DomainEvent])
113
+ broker_with_custom_dispatcher.notify_subscribers(event1)
114
+ expect(dispatcher.dispatched).to eq([{subscriber: handler, event: event1}])
115
+ end
116
+
117
+ end
@@ -1,3 +1,3 @@
1
1
  module RubyEventStore
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_event_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - rybex
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-05-25 00:00:00.000000000 Z
12
+ date: 2016-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -105,6 +105,7 @@ files:
105
105
  - lib/ruby_event_store/in_memory_repository.rb
106
106
  - lib/ruby_event_store/projection.rb
107
107
  - lib/ruby_event_store/pub_sub/broker.rb
108
+ - lib/ruby_event_store/spec/event_broker_lint.rb
108
109
  - lib/ruby_event_store/spec/event_repository_lint.rb
109
110
  - lib/ruby_event_store/version.rb
110
111
  - ruby_event_store.gemspec
@@ -127,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  version: '0'
128
129
  requirements: []
129
130
  rubyforge_project:
130
- rubygems_version: 2.2.3
131
+ rubygems_version: 2.5.1
131
132
  signing_key:
132
133
  specification_version: 4
133
134
  summary: Event Store in Ruby