event_bus 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source :rubygems
2
+ gemspec
3
+
4
+ group :test do
5
+ gem 'rspec', '~> 2.12'
6
+ gem 'rake', '~> 0.9.2'
7
+ gem 'simplecov', :require => false
8
+ end
9
+
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ event_bus (0.0.2)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.3)
10
+ multi_json (1.5.0)
11
+ rake (0.9.2.2)
12
+ rspec (2.12.0)
13
+ rspec-core (~> 2.12.0)
14
+ rspec-expectations (~> 2.12.0)
15
+ rspec-mocks (~> 2.12.0)
16
+ rspec-core (2.12.2)
17
+ rspec-expectations (2.12.1)
18
+ diff-lcs (~> 1.1.3)
19
+ rspec-mocks (2.12.1)
20
+ simplecov (0.7.1)
21
+ multi_json (~> 1.0)
22
+ simplecov-html (~> 0.7.1)
23
+ simplecov-html (0.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ event_bus!
30
+ rake (~> 0.9.2)
31
+ rspec (~> 2.12)
32
+ simplecov
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # EventBus
2
+
3
+ A simple pubsub event bus for Ruby applications.
4
+
5
+ [![Build Status](https://travis-ci.org/kevinrutherford/event_bus.png)](https://travis-ci.org/kevinrutherford/event_bus)
6
+
7
+ * <https://rubygems.org/gems/event_bus>
8
+ * <http://rubydoc.info/gems/event_bus/frames>
9
+ * <https://github.com/kevinrutherford/event_bus>
10
+
11
+ ## Features
12
+
13
+ * Simple, global support for the Observer pattern, aka Publisher-Subscriber.
14
+ * Publish and subscribe to events throughout your Ruby application.
15
+ * Listen for events without coupling to the publishing object or class.
16
+ * Subscribe to events using names or regex patterns.
17
+ * Works with Rails.
18
+
19
+ ## Installation
20
+
21
+ Install the gem
22
+
23
+ ```
24
+ gem install event_bus
25
+ ```
26
+
27
+ Or add it to your Gemfile and run `bundle`.
28
+
29
+ ``` ruby
30
+ gem 'event_bus'
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ Subscribe to an event in your application's initialization code:
36
+
37
+ ```ruby
38
+ EventBus.listen_for('order-placed', StatsRecorder.new, :order_placed)
39
+ ```
40
+
41
+ Fire the event whenever something significant happens in your application:
42
+
43
+ ```ruby
44
+ class PlaceOrder
45
+ //...
46
+ EventBus.listen_for('order-placed', :order => current_order, :customer => current_user)
47
+ end
48
+ ```
49
+
50
+ Handle the event without coupling the publisher or subscriber:
51
+
52
+ ```ruby
53
+ class StatsRecorder
54
+ def order_placed(details)
55
+ order = details[:order]
56
+ //...
57
+ end
58
+ end
59
+ ```
60
+
61
+ ## Compatibility
62
+
63
+ Tested with Ruby 1.8.7, 1.9.x, JRuby, Rubinius.
64
+ See the [build status](https://travis-ci.org/kevinrutherford/event_bus)
65
+ for details.
66
+
67
+ ## License
68
+
69
+ (The MIT License)
70
+
71
+ Copyright (c) 2013 Kevin Rutherford
72
+
73
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
74
+ this software and associated documentation files (the 'Software'), to deal in
75
+ the Software without restriction, including without limitation the rights to
76
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
77
+ of the Software, and to permit persons to whom the Software is furnished to do
78
+ so, subject to the following conditions:
79
+
80
+ The above copyright notice and this permission notice shall be included in all
81
+ copies or substantial portions of the Software.
82
+
83
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
84
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
85
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
86
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
87
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
88
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
89
+ SOFTWARE.
90
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new('spec')
4
+
5
+ task :default => :spec
6
+
data/lib/event_bus.rb CHANGED
@@ -52,12 +52,6 @@ class EventBus
52
52
  class Registrations
53
53
  include Singleton
54
54
 
55
- Registration = Struct.new(:pattern, :listener, :method_name) do
56
- def respond(event_name, details)
57
- listener.send(method_name, details) if pattern === event_name
58
- end
59
- end
60
-
61
55
  def initialize
62
56
  clear
63
57
  end
@@ -76,6 +70,14 @@ class EventBus
76
70
  @listeners << Registration.new(pattern, listener, method_name)
77
71
  end
78
72
 
73
+ private
74
+
75
+ Registration = Struct.new(:pattern, :listener, :method_name) do
76
+ def respond(event_name, details)
77
+ listener.send(method_name, details) if pattern === event_name
78
+ end
79
+ end
80
+
79
81
  end
80
82
 
81
83
  end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe EventBus do
4
+ let(:listener) { double(:listener) }
5
+ let(:event_name) { 'aa123bb' }
6
+ let(:receiving_method) { :receiving_method_name }
7
+
8
+ before do
9
+ EventBus.clear
10
+ end
11
+
12
+ describe '.announce' do
13
+
14
+ it 'returns itself, to facilitate cascades' do
15
+ EventBus.clear.should == EventBus
16
+ end
17
+
18
+ context 'when the listener is specific about the event name' do
19
+ it 'sends the event to the listener' do
20
+ EventBus.listen_for(event_name, listener, receiving_method)
21
+ listener.should_receive(receiving_method).with({:a => 1, :b => 2})
22
+ EventBus.announce(event_name, {:a => 1, :b => 2})
23
+ end
24
+ end
25
+
26
+ context 'when the listener uses a regex that matches' do
27
+ it 'sends the event to the listener' do
28
+ EventBus.listen_for(/123b/, listener, receiving_method)
29
+ listener.should_receive(receiving_method).with({:a => 1, :b => 2})
30
+ EventBus.announce(event_name, {:a => 1, :b => 2})
31
+ end
32
+ end
33
+
34
+ context 'when the listener listens for a different event' do
35
+ it 'does not send the event to the listener' do
36
+ EventBus.listen_for('blah', listener, receiving_method)
37
+ listener.should_not_receive(receiving_method)
38
+ EventBus.announce(event_name, {:a => 1, :b => 2})
39
+ end
40
+ end
41
+
42
+ context 'when the listener listens for a non-matching regex' do
43
+ it 'does not send the event to the listener' do
44
+ EventBus.listen_for(/123a/, listener, receiving_method)
45
+ listener.should_not_receive(receiving_method)
46
+ EventBus.announce(event_name, {:a => 1, :b => 2})
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ describe '.clear' do
53
+ it 'sends no event to previous registrants' do
54
+ EventBus.listen_for(event_name, listener, receiving_method)
55
+ EventBus.clear
56
+ listener.should_not_receive(receiving_method)
57
+ EventBus.announce(event_name, {})
58
+ end
59
+
60
+ it 'returns itself, to facilitate cascades' do
61
+ EventBus.clear.should == EventBus
62
+ end
63
+ end
64
+
65
+ end
66
+
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'simplecov'
4
+
5
+ SimpleCov.start
6
+
7
+ require 'event_bus'
8
+
9
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,15 +9,70 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-21 00:00:00.000000000 Z
13
- dependencies: []
14
- description: A simple pubsub event bus
12
+ date: 2013-01-24 00:00:00.000000000 Z
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.9.2
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.9.2
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.12'
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
+ description: event_bus provides support for application-wide events, without coupling
63
+ the publishing and subscribing objects or classes to each other
15
64
  email: kevin@rutherford-software.com
16
65
  executables: []
17
66
  extensions: []
18
67
  extra_rdoc_files: []
19
68
  files:
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - README.md
72
+ - Rakefile
20
73
  - lib/event_bus.rb
74
+ - spec/lib/event_bus_spec.rb
75
+ - spec/spec_helper.rb
21
76
  homepage: http://github.com/kevinrutherford/event_bus
22
77
  licenses: []
23
78
  post_install_message:
@@ -41,6 +96,8 @@ rubyforge_project:
41
96
  rubygems_version: 1.8.24
42
97
  signing_key:
43
98
  specification_version: 3
44
- summary: A simple pubsub event bus
45
- test_files: []
99
+ summary: A simple pubsub event bus for Ruby applications
100
+ test_files:
101
+ - spec/lib/event_bus_spec.rb
102
+ - spec/spec_helper.rb
46
103
  has_rdoc: