event_bus 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/event_bus.rb +56 -23
- metadata +4 -3
data/lib/event_bus.rb
CHANGED
@@ -1,48 +1,81 @@
|
|
1
1
|
require 'singleton'
|
2
2
|
|
3
3
|
class EventBus
|
4
|
-
include Singleton
|
5
4
|
|
5
|
+
#
|
6
|
+
# Announce an event to any waiting listeners.
|
7
|
+
#
|
8
|
+
# @param event_name [String, Symbol] the name of your event
|
9
|
+
# @param details [Hash] the information you want to pass to the listeners
|
10
|
+
# @return the EventBus, ready to be called again.
|
11
|
+
#
|
6
12
|
def self.announce(event_name, details)
|
7
|
-
instance.announce(event_name, details)
|
13
|
+
Registrations.instance.announce(event_name, details)
|
14
|
+
self
|
8
15
|
end
|
9
16
|
|
10
|
-
|
11
|
-
|
17
|
+
#
|
18
|
+
# Register a single listener.
|
19
|
+
#
|
20
|
+
# @param pattern [String, Regex] listen for any events whose name matches this pattern
|
21
|
+
# @param listener the object to be notified when a matching event occurs
|
22
|
+
# @param method_name [Symbol] the method to be called on listener when a matching event occurs
|
23
|
+
# @return the EventBus, ready to be called again.
|
24
|
+
#
|
25
|
+
def self.listen_for(pattern, listener, method_name)
|
26
|
+
Registrations.instance.register(pattern, listener, method_name)
|
27
|
+
self
|
12
28
|
end
|
13
29
|
|
14
|
-
|
15
|
-
|
30
|
+
#
|
31
|
+
# Delete all current listener registrations
|
32
|
+
#
|
33
|
+
# @return the EventBus, ready to be called again.
|
34
|
+
#
|
35
|
+
def self.clear
|
36
|
+
Registrations.instance.clear
|
37
|
+
self
|
16
38
|
end
|
17
39
|
|
40
|
+
#
|
41
|
+
# (experimental)
|
42
|
+
#
|
18
43
|
def self.register(listener)
|
19
44
|
listener.events_map.each do |pattern, method_name|
|
20
|
-
instance.register(pattern, listener, method_name)
|
45
|
+
Registrations.instance.register(pattern, listener, method_name)
|
21
46
|
end
|
47
|
+
self
|
22
48
|
end
|
23
49
|
|
24
|
-
|
25
|
-
|
26
|
-
|
50
|
+
private
|
51
|
+
|
52
|
+
class Registrations
|
53
|
+
include Singleton
|
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
|
27
59
|
end
|
28
|
-
end
|
29
60
|
|
30
|
-
|
31
|
-
|
32
|
-
|
61
|
+
def initialize
|
62
|
+
clear
|
63
|
+
end
|
33
64
|
|
34
|
-
|
35
|
-
|
36
|
-
|
65
|
+
def announce(event_name, details)
|
66
|
+
@listeners.each do |listener|
|
67
|
+
listener.respond(event_name, details)
|
68
|
+
end
|
37
69
|
end
|
38
|
-
end
|
39
70
|
|
40
|
-
|
41
|
-
|
42
|
-
|
71
|
+
def clear
|
72
|
+
@listeners = []
|
73
|
+
end
|
74
|
+
|
75
|
+
def register(pattern, listener, method_name)
|
76
|
+
@listeners << Registration.new(pattern, listener, method_name)
|
77
|
+
end
|
43
78
|
|
44
|
-
def register(pattern, listener, method_name)
|
45
|
-
@listeners << Registration.new(pattern, listener, method_name)
|
46
79
|
end
|
47
80
|
|
48
81
|
end
|
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.
|
4
|
+
version: 0.0.2
|
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-01-
|
12
|
+
date: 2013-01-21 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A simple pubsub event bus
|
15
15
|
email: kevin@rutherford-software.com
|
@@ -18,7 +18,7 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/event_bus.rb
|
21
|
-
homepage: http://
|
21
|
+
homepage: http://github.com/kevinrutherford/event_bus
|
22
22
|
licenses: []
|
23
23
|
post_install_message:
|
24
24
|
rdoc_options: []
|
@@ -43,3 +43,4 @@ signing_key:
|
|
43
43
|
specification_version: 3
|
44
44
|
summary: A simple pubsub event bus
|
45
45
|
test_files: []
|
46
|
+
has_rdoc:
|