event_bus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/event_bus.rb +49 -0
  2. metadata +45 -0
data/lib/event_bus.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'singleton'
2
+
3
+ class EventBus
4
+ include Singleton
5
+
6
+ def self.announce(event_name, details)
7
+ instance.announce(event_name, details)
8
+ end
9
+
10
+ def self.clear
11
+ instance.clear
12
+ end
13
+
14
+ def self.listen_for(pattern, listener, method_name)
15
+ instance.register(pattern, listener, method_name)
16
+ end
17
+
18
+ def self.register(listener)
19
+ listener.events_map.each do |pattern, method_name|
20
+ instance.register(pattern, listener, method_name)
21
+ end
22
+ end
23
+
24
+ Registration = Struct.new(:pattern, :listener, :method_name) do
25
+ def respond(event_name, details)
26
+ listener.send(method_name, details) if pattern === event_name
27
+ end
28
+ end
29
+
30
+ def initialize
31
+ clear
32
+ end
33
+
34
+ def announce(event_name, details)
35
+ @listeners.each do |listener|
36
+ listener.respond(event_name, details)
37
+ end
38
+ end
39
+
40
+ def clear
41
+ @listeners = []
42
+ end
43
+
44
+ def register(pattern, listener, method_name)
45
+ @listeners << Registration.new(pattern, listener, method_name)
46
+ end
47
+
48
+ end
49
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: event_bus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kevin Rutherford
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple pubsub event bus
15
+ email: kevin@rutherford-software.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/event_bus.rb
21
+ homepage: http://rubygems.org/gems/event_bus
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: A simple pubsub event bus
45
+ test_files: []