wisper 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/wisper.rb +4 -0
- data/lib/wisper/global_listeners.rb +9 -3
- data/lib/wisper/publisher.rb +2 -2
- data/lib/wisper/version.rb +1 -1
- data/spec/lib/global_subscribers_spec.rb +16 -8
- data/spec/lib/wisper/publisher_spec.rb +28 -2
- data/spec/lib/wisper_spec.rb +5 -7
- data/spec/spec_helper.rb +9 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -202,7 +202,7 @@ However it means that when looking at the code it will not be obvious that the
|
|
202
202
|
global listeners are being executed in additional to the regular listeners.
|
203
203
|
|
204
204
|
```ruby
|
205
|
-
Wisper
|
205
|
+
Wisper.add_listener(MyListener.new)
|
206
206
|
```
|
207
207
|
|
208
208
|
In a Rails app you might want to add your global listeners in an initalizer.
|
data/lib/wisper.rb
CHANGED
@@ -11,7 +11,7 @@ module Wisper
|
|
11
11
|
@mutex = Mutex.new
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
14
|
+
def add(listener, options = {})
|
15
15
|
with_mutex { @registrations << ObjectRegistration.new(listener, options) }
|
16
16
|
self
|
17
17
|
end
|
@@ -28,8 +28,8 @@ module Wisper
|
|
28
28
|
with_mutex { @registrations.clear }
|
29
29
|
end
|
30
30
|
|
31
|
-
def self.
|
32
|
-
instance.
|
31
|
+
def self.add(listener, options = {})
|
32
|
+
instance.add(listener, options)
|
33
33
|
end
|
34
34
|
|
35
35
|
def self.registrations
|
@@ -44,6 +44,12 @@ module Wisper
|
|
44
44
|
instance.clear
|
45
45
|
end
|
46
46
|
|
47
|
+
# remain backwards compatible
|
48
|
+
def self.add_listener(listener, options = {})
|
49
|
+
warn "[DEPRECATION] use `add` instead of `add_listener`"
|
50
|
+
add(listener, options)
|
51
|
+
end
|
52
|
+
|
47
53
|
private
|
48
54
|
|
49
55
|
def with_mutex
|
data/lib/wisper/publisher.rb
CHANGED
data/lib/wisper/version.rb
CHANGED
@@ -5,18 +5,16 @@ describe Wisper::GlobalListeners do
|
|
5
5
|
let(:local_listener) { double('listener') }
|
6
6
|
let(:publisher) { publisher_class.new }
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
describe '.add_listener' do
|
8
|
+
describe '.add' do
|
11
9
|
it 'adds given listener to every publisher' do
|
12
|
-
Wisper::GlobalListeners.
|
10
|
+
Wisper::GlobalListeners.add(global_listener)
|
13
11
|
global_listener.should_receive(:it_happened)
|
14
12
|
publisher.send(:broadcast, :it_happened)
|
15
13
|
end
|
16
14
|
|
17
15
|
it 'works along side local listeners' do
|
18
16
|
# global listener
|
19
|
-
Wisper::GlobalListeners.
|
17
|
+
Wisper::GlobalListeners.add(global_listener)
|
20
18
|
|
21
19
|
# local listener
|
22
20
|
publisher.add_listener(local_listener)
|
@@ -31,7 +29,7 @@ describe Wisper::GlobalListeners do
|
|
31
29
|
num_threads = 100
|
32
30
|
(1..num_threads).to_a.map do
|
33
31
|
Thread.new do
|
34
|
-
Wisper::GlobalListeners.
|
32
|
+
Wisper::GlobalListeners.add(Object.new)
|
35
33
|
sleep(rand) # a little chaos
|
36
34
|
end
|
37
35
|
end.each(&:join)
|
@@ -42,7 +40,7 @@ describe Wisper::GlobalListeners do
|
|
42
40
|
|
43
41
|
describe '.listeners' do
|
44
42
|
it 'returns collection of global listeners' do
|
45
|
-
Wisper::GlobalListeners.
|
43
|
+
Wisper::GlobalListeners.add(global_listener)
|
46
44
|
Wisper::GlobalListeners.listeners.should == [global_listener]
|
47
45
|
end
|
48
46
|
|
@@ -53,8 +51,18 @@ describe Wisper::GlobalListeners do
|
|
53
51
|
end
|
54
52
|
|
55
53
|
it '.clear clears all global listeners' do
|
56
|
-
Wisper::GlobalListeners.
|
54
|
+
Wisper::GlobalListeners.add(global_listener)
|
57
55
|
Wisper::GlobalListeners.clear
|
58
56
|
Wisper::GlobalListeners.listeners.should be_empty
|
59
57
|
end
|
58
|
+
|
59
|
+
describe 'backwards compatibility' do
|
60
|
+
it '.add_listener adds a listener' do
|
61
|
+
silence_warnings do
|
62
|
+
Wisper::GlobalListeners.add_listener(global_listener)
|
63
|
+
global_listener.should_receive(:it_happened)
|
64
|
+
publisher.send(:broadcast, :it_happened)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
60
68
|
end
|
@@ -90,6 +90,19 @@ describe Wisper::Publisher do
|
|
90
90
|
publisher.send(:broadcast, 'something_happened')
|
91
91
|
publisher.send(:broadcast, 'and_so_did_this')
|
92
92
|
end
|
93
|
+
|
94
|
+
it '.add_block_listener subscribes block to all listed events' do
|
95
|
+
insider.should_receive(:it_happened).twice
|
96
|
+
|
97
|
+
publisher.add_block_listener(
|
98
|
+
:on => ['something_happened', 'and_so_did_this']) do
|
99
|
+
insider.it_happened
|
100
|
+
end
|
101
|
+
|
102
|
+
publisher.send(:broadcast, 'something_happened')
|
103
|
+
publisher.send(:broadcast, 'and_so_did_this')
|
104
|
+
publisher.send(:broadcast, 'but_not_this')
|
105
|
+
end
|
93
106
|
end
|
94
107
|
|
95
108
|
it 'returns publisher so methods can be chained' do
|
@@ -99,8 +112,9 @@ describe Wisper::Publisher do
|
|
99
112
|
end
|
100
113
|
|
101
114
|
describe '.on (alternative block syntax)' do
|
115
|
+
let(:insider) { double('insider') }
|
116
|
+
|
102
117
|
it 'subscribes given block to an event' do
|
103
|
-
insider = double('insider')
|
104
118
|
insider.should_receive(:it_happened)
|
105
119
|
|
106
120
|
publisher.on(:something_happened) do
|
@@ -109,12 +123,24 @@ describe Wisper::Publisher do
|
|
109
123
|
|
110
124
|
publisher.send(:broadcast, 'something_happened')
|
111
125
|
end
|
126
|
+
|
127
|
+
it 'subscribes given block to multiple events' do
|
128
|
+
insider.should_receive(:it_happened).twice
|
129
|
+
|
130
|
+
publisher.on(:something_happened, :and_so_did_this) do
|
131
|
+
insider.it_happened
|
132
|
+
end
|
133
|
+
|
134
|
+
publisher.send(:broadcast, 'something_happened')
|
135
|
+
publisher.send(:broadcast, 'and_so_did_this')
|
136
|
+
publisher.send(:broadcast, 'but_not_this')
|
137
|
+
end
|
112
138
|
end
|
113
139
|
|
114
140
|
describe '.broadcast' do
|
115
141
|
it 'does not publish events which cannot be responded to' do
|
116
142
|
listener.should_not_receive(:so_did_this)
|
117
|
-
listener.stub(:respond_to
|
143
|
+
listener.stub(:respond_to? => false)
|
118
144
|
|
119
145
|
publisher.add_listener(listener, :on => 'so_did_this')
|
120
146
|
|
data/spec/lib/wisper_spec.rb
CHANGED
@@ -22,12 +22,10 @@ describe Wisper do
|
|
22
22
|
|
23
23
|
publisher.send(:broadcast, 'not_here')
|
24
24
|
end
|
25
|
-
end
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
$VERBOSE = original_verbosity
|
26
|
+
it '.add_listener adds a global listener' do
|
27
|
+
listener = double('listener')
|
28
|
+
Wisper.add_listener(listener)
|
29
|
+
Wisper::GlobalListeners.listeners.should == [listener]
|
30
|
+
end
|
33
31
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,9 +8,18 @@ RSpec.configure do |config|
|
|
8
8
|
config.run_all_when_everything_filtered = true
|
9
9
|
config.filter_run :focus
|
10
10
|
config.order = 'random'
|
11
|
+
config.after(:each) { Wisper::GlobalListeners.clear }
|
11
12
|
end
|
12
13
|
|
13
14
|
# returns an anonymous wispered class
|
14
15
|
def publisher_class
|
15
16
|
Class.new { include Wisper::Publisher }
|
16
17
|
end
|
18
|
+
|
19
|
+
# prevents deprecation warning showing up in spec output
|
20
|
+
def silence_warnings
|
21
|
+
original_verbosity = $VERBOSE
|
22
|
+
$VERBOSE = nil
|
23
|
+
yield
|
24
|
+
$VERBOSE = original_verbosity
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wisper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|