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 CHANGED
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
+ bundler_args: --without=metrics
3
+ script: rspec spec
2
4
  rvm:
3
5
  - '1.9.2'
4
6
  - '1.9.3'
data/Gemfile CHANGED
@@ -2,9 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- group :development do
6
- gem 'guard'
7
- gem 'guard-rspec'
8
- gem 'rb-fsevent', '~>0.9.1' if RUBY_PLATFORM =~ /darwin/
5
+ group :metrics do
6
+ gem 'simplecov'
9
7
  gem 'flay'
10
8
  end
data/README.md CHANGED
@@ -1,32 +1,28 @@
1
1
  # Wisper
2
2
 
3
- Simple pub/sub for Ruby objects
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
- While this is not dependent on Rails in any way it was extracted from a Rails
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
- The problem with callbacks and Observers is that they always happen. How many
12
- times have you wanted to do `User.create` without firing off a welcome email?
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.0.0'
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 to subscribed
29
- listeners. Listeners are added, at runtime, to the publishing object.
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, self)
35
+ publish(:done_something)
40
36
  end
41
37
  end
42
38
  ```
43
39
 
44
- When the publisher publishes an event it can pass any number of arguments which
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, self, 'hello', 'world')
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 by default they are only subscribed to events
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
- def execute(player, team)
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(player, team)
139
- assign_first_mission(player, team)
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(player, team)
150
+ def email_player
149
151
  # ...
150
152
  end
151
153
 
152
- def assign_first_mission(player, team)
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 global listeners. They receive all published events which they can respond
200
+ add listeners globally. They receive all broadcast events which they can respond
199
201
  to.
200
202
 
201
- However it means that when looking at the code it will not be obvious that the
202
- global listeners are being executed in additional to the regular listeners.
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
- In the above case it is pretty useless unless used in conjuction with `:on`
248
- since all events will get mapped to `:successful`. Instead you might do
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 isolation tests
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
@@ -17,7 +17,7 @@ module Wisper
17
17
  TemporaryListeners.with(*args, &block)
18
18
  end
19
19
 
20
- def self.add_listener(listener)
21
- GlobalListeners.add(listener)
20
+ def self.add_listener(listener, options = {})
21
+ GlobalListeners.add(listener, options)
22
22
  end
23
23
  end
@@ -1,3 +1,3 @@
1
1
  module Wisper
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
@@ -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
@@ -1,5 +1,8 @@
1
- require 'simplecov'
2
- SimpleCov.start
1
+ begin
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+ rescue LoadError
5
+ end
3
6
 
4
7
  require 'wisper'
5
8
 
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.0
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-21 00:00:00.000000000 Z
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: