wisper 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +2 -0
- data/Gemfile +2 -4
- data/README.md +45 -33
- data/lib/wisper.rb +2 -2
- data/lib/wisper/version.rb +1 -1
- data/spec/lib/global_subscribers_spec.rb +9 -0
- data/spec/spec_helper.rb +5 -2
- data/wisper.gemspec +1 -2
- metadata +4 -35
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,32 +1,28 @@
|
|
1
1
|
# Wisper
|
2
2
|
|
3
|
-
|
3
|
+
Wisper is a Ruby library for decoupling and managing the dependencies of your
|
4
|
+
Ruby objects using Pub/Sub.
|
4
5
|
|
5
6
|
[![Code Climate](https://codeclimate.com/github/krisleech/wisper.png)](https://codeclimate.com/github/krisleech/wisper)
|
6
7
|
[![Build Status](https://travis-ci.org/krisleech/wisper.png?branch=master)](https://travis-ci.org/krisleech/wisper)
|
7
8
|
|
8
|
-
|
9
|
-
project and can used as an alternative to ActiveRecord callbacks and Observers.
|
9
|
+
Wisper was extracted from a Rails codebase but is not dependant on Rails.
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
It is also super useful for integrating web socket notifications, statistics
|
15
|
-
and activity streams in to your controller layer without coupling them to your
|
16
|
-
models.
|
11
|
+
It is commonly used as an alternative to ActiveRecord callbacks and Observers
|
12
|
+
to reduce coupling between data and domain layers.
|
17
13
|
|
18
14
|
## Installation
|
19
15
|
|
20
16
|
Add this line to your application's Gemfile:
|
21
17
|
|
22
18
|
```ruby
|
23
|
-
gem 'wisper', '~>1.
|
19
|
+
gem 'wisper', '~>1.2.0'
|
24
20
|
```
|
25
21
|
|
26
22
|
## Usage
|
27
23
|
|
28
|
-
Any class with the Wisper module included can broadcast events
|
29
|
-
listeners. Listeners
|
24
|
+
Any class with the `Wisper::Publisher` module included can broadcast events
|
25
|
+
to subscribed listeners. Listeners subscribe, at runtime, to the publisher.
|
30
26
|
|
31
27
|
### Publishing
|
32
28
|
|
@@ -36,24 +32,23 @@ class MyPublisher
|
|
36
32
|
|
37
33
|
def do_something
|
38
34
|
# ...
|
39
|
-
publish(:done_something
|
35
|
+
publish(:done_something)
|
40
36
|
end
|
41
37
|
end
|
42
38
|
```
|
43
39
|
|
44
|
-
When
|
45
|
-
are passed on to the listeners.
|
40
|
+
When a publisher broadcasts an event it can pass any number of arguments which
|
41
|
+
are to be passed on to the listeners.
|
46
42
|
|
47
43
|
```ruby
|
48
|
-
publish(:done_something,
|
44
|
+
publish(:done_something, 'hello', 'world')
|
49
45
|
```
|
50
46
|
|
51
47
|
### Subscribing
|
52
48
|
|
53
49
|
#### Listeners
|
54
50
|
|
55
|
-
Any object can be a listener and
|
56
|
-
they can respond to.
|
51
|
+
Any object can be a listener and only receives events it can respond to.
|
57
52
|
|
58
53
|
```ruby
|
59
54
|
my_publisher = MyPublisher.new
|
@@ -62,7 +57,7 @@ my_publisher.subscribe(MyListener.new)
|
|
62
57
|
|
63
58
|
#### Blocks
|
64
59
|
|
65
|
-
Blocks are subscribed to single events.
|
60
|
+
Blocks are subscribed to single events only.
|
66
61
|
|
67
62
|
```ruby
|
68
63
|
my_publisher = MyPublisher.new
|
@@ -130,13 +125,20 @@ responsibility.
|
|
130
125
|
class PlayerJoiningTeam
|
131
126
|
include Wisper::Publisher
|
132
127
|
|
133
|
-
|
128
|
+
attr_reader :player, :team
|
129
|
+
|
130
|
+
def initialize(player, team)
|
131
|
+
@player = player
|
132
|
+
@team = team
|
133
|
+
end
|
134
|
+
|
135
|
+
def execute
|
134
136
|
membership = Membership.new(player, team)
|
135
137
|
|
136
138
|
if membership.valid?
|
137
139
|
membership.save!
|
138
|
-
email_player
|
139
|
-
assign_first_mission
|
140
|
+
email_player
|
141
|
+
assign_first_mission
|
140
142
|
publish(:player_joining_team_successful, player, team)
|
141
143
|
else
|
142
144
|
publish(:player_joining_team_failed, player, team)
|
@@ -145,11 +147,11 @@ class PlayerJoiningTeam
|
|
145
147
|
|
146
148
|
private
|
147
149
|
|
148
|
-
def email_player
|
150
|
+
def email_player
|
149
151
|
# ...
|
150
152
|
end
|
151
153
|
|
152
|
-
def assign_first_mission
|
154
|
+
def assign_first_mission
|
153
155
|
# ...
|
154
156
|
end
|
155
157
|
end
|
@@ -195,11 +197,12 @@ end
|
|
195
197
|
## Global listeners
|
196
198
|
|
197
199
|
If you become tired of adding the same listeners to _every_ publisher you can
|
198
|
-
add
|
200
|
+
add listeners globally. They receive all broadcast events which they can respond
|
199
201
|
to.
|
200
202
|
|
201
|
-
|
202
|
-
|
203
|
+
Global listeners should be used with caution, the execution path becomes less
|
204
|
+
obvious on reading the code and of course you are introducing global state and
|
205
|
+
'always on' behaviour. This may not desirable.
|
203
206
|
|
204
207
|
```ruby
|
205
208
|
Wisper.add_listener(MyListener.new)
|
@@ -235,7 +238,7 @@ of events to `:on`.
|
|
235
238
|
post_creater.subscribe(PusherListener.new, :on => :create_post_successful)
|
236
239
|
```
|
237
240
|
|
238
|
-
## Mapping event to a different method
|
241
|
+
## Mapping an event to a different method
|
239
242
|
|
240
243
|
By default the method called on the subscriber is the same as the event
|
241
244
|
broadcast. However it can be mapped to a different method using `:with`.
|
@@ -244,9 +247,8 @@ broadcast. However it can be mapped to a different method using `:with`.
|
|
244
247
|
report_creator.subscribe(MailResponder.new, :with => :successful)
|
245
248
|
```
|
246
249
|
|
247
|
-
|
248
|
-
|
249
|
-
something like this:
|
250
|
+
This is pretty useless unless used in conjuction with `:on`, since all events
|
251
|
+
will get mapped to `:successful`. Instead you might do something like this:
|
250
252
|
|
251
253
|
```ruby
|
252
254
|
report_creator.subscribe(MailResponder.new, :on => :create_report_successful,
|
@@ -275,8 +277,8 @@ post.on(:success) { |post| redirect_to post }
|
|
275
277
|
|
276
278
|
## RSpec
|
277
279
|
|
278
|
-
Wisper comes with a method for stubbing event publishers so that you can create
|
279
|
-
that only care about reacting to events.
|
280
|
+
Wisper comes with a method for stubbing event publishers so that you can create
|
281
|
+
isolation tests that only care about reacting to events.
|
280
282
|
|
281
283
|
Given this piece of code:
|
282
284
|
|
@@ -328,8 +330,18 @@ See `spec/lib/rspec_extensions_spec.rb` for a runnable example.
|
|
328
330
|
|
329
331
|
Tested with MRI 1.9.x, MRI 2.0.0, JRuby (1.9 and 2.0 mode) and Rubinius (1.9
|
330
332
|
mode).
|
333
|
+
|
331
334
|
See the [build status](https://travis-ci.org/krisleech/wisper) for details.
|
332
335
|
|
336
|
+
## Running Specs
|
337
|
+
|
338
|
+
```
|
339
|
+
rspec spec
|
340
|
+
```
|
341
|
+
|
342
|
+
There is both a `Rakefile` and `Guardfile`, if you like you prefer to run the
|
343
|
+
specs using `guard-rspec` or `rake`.
|
344
|
+
|
333
345
|
## License
|
334
346
|
|
335
347
|
(The MIT License)
|
data/lib/wisper.rb
CHANGED
data/lib/wisper/version.rb
CHANGED
@@ -12,6 +12,15 @@ describe Wisper::GlobalListeners do
|
|
12
12
|
publisher.send(:broadcast, :it_happened)
|
13
13
|
end
|
14
14
|
|
15
|
+
it 'works with options' do
|
16
|
+
Wisper::GlobalListeners.add(global_listener, :on => :it_happened,
|
17
|
+
:with => :woot)
|
18
|
+
global_listener.should_receive(:woot).once
|
19
|
+
global_listener.should_not_receive(:it_happened_again)
|
20
|
+
publisher.send(:broadcast, :it_happened)
|
21
|
+
publisher.send(:broadcast, :it_happened_again)
|
22
|
+
end
|
23
|
+
|
15
24
|
it 'works along side local listeners' do
|
16
25
|
# global listener
|
17
26
|
Wisper::GlobalListeners.add(global_listener)
|
data/spec/spec_helper.rb
CHANGED
data/wisper.gemspec
CHANGED
@@ -11,13 +11,12 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.description = %q{pub/sub for Ruby objects}
|
12
12
|
gem.summary = %q{pub/sub for Ruby objects}
|
13
13
|
gem.homepage = "https://github.com/krisleech/wisper"
|
14
|
+
gem.license = "MIT"
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($/)
|
16
17
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
19
|
gem.require_paths = ["lib"]
|
19
20
|
|
20
|
-
gem.add_development_dependency 'rake'
|
21
21
|
gem.add_development_dependency 'rspec'
|
22
|
-
gem.add_development_dependency 'simplecov'
|
23
22
|
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.2.
|
4
|
+
version: 1.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,24 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07
|
12
|
+
date: 2013-10-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: rake
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :development
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
30
14
|
- !ruby/object:Gem::Dependency
|
31
15
|
name: rspec
|
32
16
|
requirement: !ruby/object:Gem::Requirement
|
@@ -43,22 +27,6 @@ dependencies:
|
|
43
27
|
- - ! '>='
|
44
28
|
- !ruby/object:Gem::Version
|
45
29
|
version: '0'
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: simplecov
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ! '>='
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '0'
|
54
|
-
type: :development
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
|
-
requirements:
|
59
|
-
- - ! '>='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
30
|
description: pub/sub for Ruby objects
|
63
31
|
email:
|
64
32
|
- kris.leech@gmail.com
|
@@ -93,7 +61,8 @@ files:
|
|
93
61
|
- spec/spec_helper.rb
|
94
62
|
- wisper.gemspec
|
95
63
|
homepage: https://github.com/krisleech/wisper
|
96
|
-
licenses:
|
64
|
+
licenses:
|
65
|
+
- MIT
|
97
66
|
post_install_message:
|
98
67
|
rdoc_options: []
|
99
68
|
require_paths:
|