ribbon-event_bus 0.3.0 → 0.4.0
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.
- checksums.yaml +4 -4
- data/lib/ribbon/event_bus/errors.rb +1 -0
- data/lib/ribbon/event_bus/instance.rb +7 -7
- data/lib/ribbon/event_bus/subscription.rb +35 -11
- data/lib/ribbon/event_bus/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd26c9dead029e9b0486e8ba596468db76fb7bb3
|
4
|
+
data.tar.gz: 0051194033a07c691886ce03b1f9a2ff07f83a94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b42f285a3fbca1401bff8810282a38c3392da3d56a03c9c594cd2fc2c93556a55a403ed0ab943f05bc96f040d1b90b4741e2c0b61e3e630c41a64a4d3d93c84
|
7
|
+
data.tar.gz: 2757b6055e37f0bb290d3ff98ee279d955f947f75a9de935c310ae2c91b7f5d16f9f4bf4affd2c6f917be3929901aa057f5a2a5818a94367ed858142f23ecc39
|
@@ -25,6 +25,7 @@ module Ribbon::EventBus
|
|
25
25
|
class SubscriptionError < Error; end
|
26
26
|
class InvalidPriorityError < SubscriptionError; end
|
27
27
|
class UnexpectedEventError < SubscriptionError; end
|
28
|
+
class DuplicateIdentifierError < SubscriptionError; end
|
28
29
|
|
29
30
|
###
|
30
31
|
# Publisher Errors
|
@@ -68,8 +68,8 @@ module Ribbon
|
|
68
68
|
_registered_subscriptions_to(event_name).dup
|
69
69
|
end
|
70
70
|
|
71
|
-
def find_subscription(
|
72
|
-
|
71
|
+
def find_subscription(identifier)
|
72
|
+
_subscriptions_by_identifiers[identifier]
|
73
73
|
end
|
74
74
|
|
75
75
|
def find_publisher(publisher)
|
@@ -82,11 +82,11 @@ module Ribbon
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def _register_subscription(subscription)
|
85
|
-
if
|
85
|
+
if _subscriptions_by_identifiers[subscription.identifier]
|
86
86
|
# This is not expected to occur
|
87
|
-
raise Errors::SubscriptionError, "duplicate
|
87
|
+
raise Errors::SubscriptionError, "duplicate identifier: #{subscription.identifier.inspect}"
|
88
88
|
else
|
89
|
-
|
89
|
+
_subscriptions_by_identifiers[subscription.identifier] = subscription
|
90
90
|
end
|
91
91
|
|
92
92
|
_registered_subscriptions_to(subscription.event_name)
|
@@ -116,8 +116,8 @@ module Ribbon
|
|
116
116
|
(@__registered_subscriptions ||= {})[event_name] ||= []
|
117
117
|
end
|
118
118
|
|
119
|
-
def
|
120
|
-
@
|
119
|
+
def _subscriptions_by_identifiers
|
120
|
+
@__registered_subscriptions_by_identifier ||= {}
|
121
121
|
end
|
122
122
|
|
123
123
|
def _load_default_config
|
@@ -7,12 +7,12 @@ module Ribbon::EventBus
|
|
7
7
|
include Mixins::Serializable
|
8
8
|
|
9
9
|
config_key :subscriptions
|
10
|
-
serialize_with :instance, :
|
10
|
+
serialize_with :instance, :identifier
|
11
11
|
|
12
12
|
attr_reader :name
|
13
13
|
attr_reader :event_name
|
14
14
|
attr_reader :priority
|
15
|
-
attr_reader :
|
15
|
+
attr_reader :identifier
|
16
16
|
|
17
17
|
def initialize(event_name, params={}, &block)
|
18
18
|
@event_name = event_name.to_sym
|
@@ -20,14 +20,17 @@ module Ribbon::EventBus
|
|
20
20
|
|
21
21
|
_evaluate_params(params)
|
22
22
|
|
23
|
-
@
|
24
|
-
@locator = _generate_locator
|
23
|
+
@identifier = _generate_identifier
|
25
24
|
|
26
|
-
instance.
|
25
|
+
if instance.find_subscription(identifier)
|
26
|
+
raise Errors::DuplicateIdentifierError, "give this subscription a unique name"
|
27
|
+
else
|
28
|
+
instance._register_subscription(self)
|
29
|
+
end
|
27
30
|
end
|
28
31
|
|
29
|
-
def self.load_from_serialized(instance,
|
30
|
-
instance.find_subscription(
|
32
|
+
def self.load_from_serialized(instance, identifier)
|
33
|
+
instance.find_subscription(identifier)
|
31
34
|
end
|
32
35
|
|
33
36
|
def handle(event)
|
@@ -40,7 +43,7 @@ module Ribbon::EventBus
|
|
40
43
|
end
|
41
44
|
|
42
45
|
def to_s
|
43
|
-
"Subscription(#{event_name}
|
46
|
+
"Subscription(on #{event_name}: #{name || _path})"
|
44
47
|
end
|
45
48
|
|
46
49
|
private
|
@@ -49,8 +52,15 @@ module Ribbon::EventBus
|
|
49
52
|
@__path ||= _determine_path
|
50
53
|
end
|
51
54
|
|
55
|
+
##
|
56
|
+
# Determines the file path of the ruby code defining the subscription.
|
57
|
+
# It's important that this is called from within the initializer to get the
|
58
|
+
# desired effect.
|
52
59
|
def _determine_path
|
53
60
|
path = File.expand_path('../..', __FILE__)
|
61
|
+
|
62
|
+
# Will be something like:
|
63
|
+
# "/path/to/file.rb:47:in `method_name'"
|
54
64
|
non_event_bus_caller = caller.find { |c| !c.start_with?(path) }
|
55
65
|
|
56
66
|
unless non_event_bus_caller
|
@@ -61,8 +71,22 @@ module Ribbon::EventBus
|
|
61
71
|
non_event_bus_caller
|
62
72
|
end
|
63
73
|
|
64
|
-
|
65
|
-
|
74
|
+
##
|
75
|
+
# Generates a unique identifier for this subscription which will be used to
|
76
|
+
# "serialize" it.
|
77
|
+
#
|
78
|
+
# The goal here is to generate a identifier that is the same across different
|
79
|
+
# processes and servers and that ideally changes infrequently between
|
80
|
+
# application versions, but when it does change, it should do so predictably.
|
81
|
+
def _generate_identifier
|
82
|
+
# Cut off everything from the line number onward. That way, the identifier
|
83
|
+
# does not change when the subscription block moves to a different line.
|
84
|
+
index = _path.rindex(/:\d+:/) - 1
|
85
|
+
path = _path[0..index]
|
86
|
+
|
87
|
+
raise Errors::SubscriptionError, "Invalid path: #{path}" unless File.exists?(path)
|
88
|
+
|
89
|
+
Digest::MD5.hexdigest("#{path}:#{event_name}:#{name}").to_sym
|
66
90
|
end
|
67
91
|
|
68
92
|
def _symbol_to_priority(sym)
|
@@ -89,7 +113,7 @@ module Ribbon::EventBus
|
|
89
113
|
###
|
90
114
|
def _evaluate_params(params)
|
91
115
|
@instance = params[:instance]
|
92
|
-
@name = params[:name]
|
116
|
+
@name = params[:name].to_s
|
93
117
|
@priority = _evaluate_priority(params[:priority])
|
94
118
|
end
|
95
119
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ribbon-event_bus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Honer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ribbon-plugins
|
@@ -190,7 +190,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
190
190
|
version: '0'
|
191
191
|
requirements: []
|
192
192
|
rubyforge_project:
|
193
|
-
rubygems_version: 2.4.
|
193
|
+
rubygems_version: 2.4.3
|
194
194
|
signing_key:
|
195
195
|
specification_version: 4
|
196
196
|
summary: An asynchronous event bus for Ruby.
|