ribbon-event_bus 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07d4601534a5d201e75d5c81367616d82e278c2e
4
- data.tar.gz: de763f8aeb39610d210860de8050f6dd6a78327d
3
+ metadata.gz: cd26c9dead029e9b0486e8ba596468db76fb7bb3
4
+ data.tar.gz: 0051194033a07c691886ce03b1f9a2ff07f83a94
5
5
  SHA512:
6
- metadata.gz: 0d1adb38016cc6bb5c59b9d345d147f2dc224d69de9bf166bc13a1a0e4c32869a84a3f3e362a163473551f54a88b1d90d1679838e5d0fcf99488b728063d7a36
7
- data.tar.gz: 76a811606c9e92f8c87f6c9131ad7e20e8a011648729c8b86f2da4b053d2a34e5dfd1e8a347cbe50a01820837a15401c4e440d777e3b8098c3a54ce244e4eb29
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(locator)
72
- _subscriptions_by_locators[locator]
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 _subscriptions_by_locators[subscription.locator]
85
+ if _subscriptions_by_identifiers[subscription.identifier]
86
86
  # This is not expected to occur
87
- raise Errors::SubscriptionError, "duplicate locator: #{subscription.locator.inspect}"
87
+ raise Errors::SubscriptionError, "duplicate identifier: #{subscription.identifier.inspect}"
88
88
  else
89
- _subscriptions_by_locators[subscription.locator] = subscription
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 _subscriptions_by_locators
120
- @__registered_subscriptions_by_locator ||= {}
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, :locator
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 :locator
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
- @name ||= _path
24
- @locator = _generate_locator
23
+ @identifier = _generate_identifier
25
24
 
26
- instance._register_subscription(self)
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, locator)
30
- instance.find_subscription(locator)
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}, #{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
- def _generate_locator
65
- Digest::MD5.hexdigest(_path).to_sym
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
 
@@ -1,5 +1,5 @@
1
1
  module Ribbon
2
2
  module EventBus
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
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.3.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-03-09 00:00:00.000000000 Z
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.5
193
+ rubygems_version: 2.4.3
194
194
  signing_key:
195
195
  specification_version: 4
196
196
  summary: An asynchronous event bus for Ruby.