ribbon-event_bus 0.1.0 → 0.2.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: c83efffe844e2c1892b1e481097a5831aedf70ee
4
- data.tar.gz: 186b25081e035b0b0ce0a2eb471fea5aee74b260
3
+ metadata.gz: fa0e255bfceecf580acb711845bdf8402d212593
4
+ data.tar.gz: 107d32e1e5dd6c66597aaefae1d3dfa6a2cc13ab
5
5
  SHA512:
6
- metadata.gz: ffd5b13af305094619bfc9b9fd51fa3df095b2fee5b1a037b4f79eebd09ad0848661d78e96d27f44aadf3af882fa709cdd71895f9e253ee5ca94551220ebf54d
7
- data.tar.gz: 6c9ef7a77f6b8934edb74e206d7ae785ddf9738fe4f5d72c89602f9d8d668653a1b1030b3d60fb3dc37a71f0f05fbdce6190d138e5cb2cbc736ae9caba5c34d9
6
+ metadata.gz: 29404aa9a31933c838acd94e60d9c0f22bd8d9ea2e46739fcca65cc83dc6778527df8ac2ecc4f801c23daff2067f61465b642eb39d65da2d801f5bc256eb7900
7
+ data.tar.gz: 830ded2e0e09da4db4403dbdc228565302895f711edb5b2c117d612747f576f3797f2343ae1815b3ad5de00e166e65d81e587037ed780a04e8a2de2b6ae57c14
@@ -18,6 +18,12 @@ module Ribbon::EventBus
18
18
  end
19
19
  end
20
20
 
21
+ class UndefinedValue
22
+ def !
23
+ true
24
+ end
25
+ end
26
+
21
27
  attr_reader :name
22
28
 
23
29
  def initialize(name=nil, &block)
@@ -60,28 +66,10 @@ module Ribbon::EventBus
60
66
  elsif /^(\w+)\?$/.match(meth_str)
61
67
  !!_get($1)
62
68
  else
63
- object = _get(meth)
64
- object = _set(meth, Config.new((name ? "#{name}." : '') + meth_str)) unless object
65
- object
69
+ _get_or_create_namespace(meth)
66
70
  end
67
71
  end
68
72
 
69
- def nested(name)
70
- _nested[name.to_sym]
71
- end
72
-
73
- def nest_value(name, value)
74
- nested()
75
- end
76
-
77
- def nested_values(key)
78
- _nested_values[key.to_sym]
79
- end
80
-
81
- def nested_configs(namespace)
82
- _nested_configs[namespace.to_sym]
83
- end
84
-
85
73
  def merge_hash!(hash)
86
74
  hash.each { |k, v|
87
75
  if v.is_a?(Hash)
@@ -103,6 +91,16 @@ module Ribbon::EventBus
103
91
  _nested[key.to_sym]
104
92
  end
105
93
 
94
+ def _get_or_create_namespace(key)
95
+ object = _get(key)
96
+
97
+ if object.is_a?(UndefinedValue)
98
+ object = _set(key, Config.new((name ? "#{name}." : '') + key.to_s))
99
+ end
100
+
101
+ object
102
+ end
103
+
106
104
  def _set(key, *args, &block)
107
105
  object = _args_to_object(*args, &block)
108
106
  _nested[key.to_sym] = object
@@ -111,7 +109,7 @@ module Ribbon::EventBus
111
109
  def _add(key, *args, &block)
112
110
  raise NotAddableError, self.inspect if @_value && !@_value.is_a?(Array)
113
111
  object = _args_to_object(*args, &block)
114
- _set(key, object.is_a?(Proc) ? ProcArray.new : []) unless _get(key)
112
+ _set(key, object.is_a?(Proc) ? ProcArray.new : []) if !_get(key)
115
113
  _get(key).push(object)
116
114
  end
117
115
 
@@ -128,7 +126,7 @@ module Ribbon::EventBus
128
126
  end
129
127
 
130
128
  def _nested
131
- @_nested ||= {}
129
+ @_nested ||= Hash.new { |hash, key| hash[key] = UndefinedValue.new }
132
130
  end
133
131
  end # Config
134
132
  end # Ribbon::EventBus
@@ -41,6 +41,11 @@ module Ribbon::EventBus
41
41
  class MissingProcError < ProcPublisherError; end
42
42
  class InvalidArityError < ProcPublisherError; end
43
43
 
44
+ ###
45
+ # Plugin Errors
46
+ ###
47
+ class PluginError < Error; end
48
+
44
49
  ###
45
50
  # Serializable Errors
46
51
  ###
@@ -25,13 +25,21 @@ module Ribbon::EventBus
25
25
  end
26
26
 
27
27
  def publish
28
- instance.publishers.each { |p| p.publish(self) }
28
+ plugins.perform(:publish, self) { |event|
29
+ instance.publishers.each { |p| p.publish(event) }
30
+ }
29
31
  end
30
32
 
31
33
  def subscriptions
32
34
  instance.subscriptions_to(self)
33
35
  end
34
36
 
37
+ def to_s
38
+ "Event(#{name}" <<
39
+ (params && !params.empty? && ", #{params.inspect}" or '') <<
40
+ ")"
41
+ end
42
+
35
43
  private
36
44
 
37
45
  ############################################################################
@@ -1,3 +1,5 @@
1
+ require 'ribbon/plugins'
2
+
1
3
  module Ribbon
2
4
  module EventBus
3
5
  DEFAULT_CONFIG_PATH = File.expand_path('../../../../config/defaults.yml', __FILE__).freeze
@@ -11,9 +13,14 @@ module Ribbon
11
13
  ##############################################################################
12
14
  class Instance
13
15
  include Mixins::Serializable
16
+ include Ribbon::Plugins::ComponentMixin
14
17
 
15
18
  serialize_with :name
16
19
 
20
+ plugin_loader do |plugin|
21
+ _translate_object_to_plugin(plugin)
22
+ end
23
+
17
24
  attr_reader :name
18
25
  attr_reader :publishers
19
26
 
@@ -43,6 +50,10 @@ module Ribbon
43
50
  }
44
51
  end
45
52
 
53
+ def plugin(*args)
54
+ config { plugin(*args) }
55
+ end
56
+
46
57
  def publish(*args)
47
58
  raise Errors::NoPublishersDefinedError unless publishers && !publishers.empty?
48
59
  _args_to_event(*args).publish
@@ -75,6 +86,23 @@ module Ribbon
75
86
  end
76
87
 
77
88
  private
89
+ def _translate_object_to_plugin(object)
90
+ case object
91
+ when String, Symbol
92
+ _translate_string_to_plugin(object.to_s)
93
+ end
94
+ end
95
+
96
+ def _translate_string_to_plugin(string)
97
+ begin
98
+ Plugins.const_get(
99
+ string.split('_').map(&:capitalize).join + 'Plugin'
100
+ )
101
+ rescue
102
+ nil # Let the Plugins gem handle this.
103
+ end
104
+ end
105
+
78
106
  def _registered_subscriptions_to(event_name)
79
107
  (@__registered_subscriptions ||= {})[event_name] ||= []
80
108
  end
@@ -101,11 +129,23 @@ module Ribbon
101
129
 
102
130
  def _process_config
103
131
  @publishers = _load_publishers.dup.freeze
132
+ _update_plugins
104
133
  end
105
134
 
106
135
  def _load_publishers
107
136
  Publishers.load_for_instance(self)
108
137
  end
138
+
139
+ def _update_plugins
140
+ plugins.clear
141
+
142
+ if config.plugin?
143
+ config.plugin.each { |plugin|
144
+ plugin = [plugin] unless plugin.is_a?(Array)
145
+ plugins.add(*plugin)
146
+ }
147
+ end
148
+ end
109
149
  end # Instance
110
150
  end # EventBus
111
151
  end # Ribbon
@@ -4,6 +4,10 @@ module Ribbon
4
4
  def instance
5
5
  (defined?(@instance) && @instance) || EventBus.instance
6
6
  end
7
+
8
+ def plugins
9
+ instance.send(:plugins)
10
+ end
7
11
  end
8
12
  end
9
13
  end
@@ -0,0 +1,68 @@
1
+ require 'logger'
2
+
3
+ module Ribbon::EventBus
4
+ module Plugins
5
+ class LoggingPlugin < Plugin
6
+ config_key :logging
7
+
8
+ def logger
9
+ @_logger ||= _load_logger
10
+ end
11
+
12
+ around_publish do |event|
13
+ _run('Publishing', event) { publish }
14
+ end
15
+
16
+ around_resque_publish do |event|
17
+ _run('Publishing on Resque', event) { resque_publish }
18
+ end
19
+
20
+ around_subscription do |sub, event|
21
+ _run('Executing Subscription', sub) { subscription }
22
+ end
23
+
24
+ private
25
+ def _load_logger
26
+ if config.logger?
27
+ config.logger
28
+ else
29
+ Logger.new(STDOUT).tap { |logger|
30
+ logger.level = _load_level
31
+ }
32
+ end
33
+ end
34
+
35
+ def _load_level
36
+ case config.level
37
+ when :info, nil
38
+ Logger::INFO
39
+ when :warn
40
+ Logger::WARN
41
+ when :error
42
+ Logger::ERROR
43
+ when :fatal
44
+ Logger::FATAL
45
+ else
46
+ raise Errors::PluginError, "Invalid plugins.logging.level: #{config.level.inspect}"
47
+ end
48
+ end
49
+
50
+ def _run(subject, object, &block)
51
+ logger.debug("#{subject}: #{object}")
52
+
53
+ if config.log_exceptions?
54
+ begin
55
+ block.call
56
+ rescue Exception => e
57
+ logger.fatal("Exception raised when #{subject.downcase} #{object}: #{e.inspect}")
58
+ raise
59
+ end
60
+ else
61
+ block.call
62
+ end
63
+
64
+ logger.debug("Finished #{subject}: #{object}")
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,22 @@
1
+ require 'ribbon/plugins'
2
+
3
+ module Ribbon::EventBus
4
+ module Plugins
5
+ class Plugin < Ribbon::Plugins::Plugin
6
+ include Mixins::HasInstance
7
+ include Mixins::HasConfig
8
+
9
+ config_key :plugins
10
+
11
+ def initialize(plugins, params={})
12
+ super(plugins)
13
+ @instance = plugins.component
14
+ @_params = params || {}
15
+ end
16
+
17
+ def config
18
+ @__config ||= super.merge_hash!(@_params)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,6 @@
1
+ module Ribbon::EventBus
2
+ module Plugins
3
+ autoload(:Plugin, 'ribbon/event_bus/plugins/plugin')
4
+ autoload(:LoggingPlugin, 'ribbon/event_bus/plugins/logging_plugin')
5
+ end
6
+ end
@@ -22,17 +22,20 @@ module Ribbon::EventBus
22
22
 
23
23
  def self.perform(sub_queue_format, serialized_event)
24
24
  event = Event.deserialize(serialized_event)
25
-
26
- event.subscriptions.each { |s|
27
- SubscriptionJob.set_queue(
28
- (sub_queue_format % {
29
- event: event.name,
30
- priority: s.priority
31
- }).to_sym
32
- )
33
-
34
- Resque.enqueue(SubscriptionJob, s.serialize, event.serialize)
35
- }
25
+ instance = event.instance
26
+
27
+ instance.plugins.perform(:resque_publish, event) do |event|
28
+ event.subscriptions.each { |s|
29
+ SubscriptionJob.set_queue(
30
+ (sub_queue_format % {
31
+ event: event.name,
32
+ priority: s.priority
33
+ }).to_sym
34
+ )
35
+
36
+ Resque.enqueue(SubscriptionJob, s.serialize, event.serialize)
37
+ }
38
+ end
36
39
  end
37
40
  end
38
41
 
@@ -9,6 +9,7 @@ module Ribbon::EventBus
9
9
  config_key :subscriptions
10
10
  serialize_with :instance, :locator
11
11
 
12
+ attr_reader :name
12
13
  attr_reader :event_name
13
14
  attr_reader :priority
14
15
  attr_reader :locator
@@ -26,7 +27,9 @@ module Ribbon::EventBus
26
27
  @_block = block
27
28
 
28
29
  _evaluate_params(params)
29
- _generate_locator
30
+
31
+ @name ||= _path
32
+ @locator = _generate_locator
30
33
 
31
34
  instance._register_subscription(self)
32
35
  end
@@ -38,12 +41,23 @@ module Ribbon::EventBus
38
41
  def handle(event)
39
42
  raise Errors::UnexpectedEventError, 'wrong name' unless event.name == event_name
40
43
  raise Errors::UnexpectedEventError, 'wrong instance' unless event.instance == instance
41
- @_block.call(event)
44
+
45
+ plugins.perform(:subscription, self, event) { |subscription, event|
46
+ @_block.call(event)
47
+ }
48
+ end
49
+
50
+ def to_s
51
+ "Subscription(#{event_name}, #{name})"
42
52
  end
43
53
 
44
54
  private
45
55
 
46
- def _generate_locator
56
+ def _path
57
+ @__path ||= _determine_path
58
+ end
59
+
60
+ def _determine_path
47
61
  path = File.expand_path('../..', __FILE__)
48
62
  non_event_bus_caller = caller.find { |c| !c.start_with?(path) }
49
63
 
@@ -52,7 +66,11 @@ module Ribbon::EventBus
52
66
  raise Errors::SubscriptionError, "Could not find non-EventBus caller"
53
67
  end
54
68
 
55
- @locator = Digest::MD5.hexdigest(non_event_bus_caller).to_sym
69
+ non_event_bus_caller
70
+ end
71
+
72
+ def _generate_locator
73
+ Digest::MD5.hexdigest(_path).to_sym
56
74
  end
57
75
 
58
76
  ############################################################################
@@ -65,9 +83,9 @@ module Ribbon::EventBus
65
83
  # Root evaluation method.
66
84
  ###
67
85
  def _evaluate_params(params)
68
- params = params.dup
69
- @instance = params.delete(:instance)
70
- @priority = _evaluate_priority(params.delete(:priority))
86
+ @instance = params[:instance]
87
+ @name = params[:name]
88
+ @priority = _evaluate_priority(params[:priority])
71
89
  end
72
90
 
73
91
  ###
@@ -1,5 +1,5 @@
1
1
  module Ribbon
2
2
  module EventBus
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,12 +1,13 @@
1
1
  module Ribbon
2
2
  module EventBus
3
- autoload(:Instance, 'ribbon/event_bus/instance')
4
- autoload(:Errors, 'ribbon/event_bus/errors')
5
- autoload(:Config, 'ribbon/event_bus/config')
6
- autoload(:Event, 'ribbon/event_bus/event')
3
+ autoload(:Instance, 'ribbon/event_bus/instance')
4
+ autoload(:Config, 'ribbon/event_bus/config')
5
+ autoload(:Publishers, 'ribbon/event_bus/publishers')
6
+ autoload(:Plugins, 'ribbon/event_bus/plugins')
7
+ autoload(:Event, 'ribbon/event_bus/event')
7
8
  autoload(:Subscription, 'ribbon/event_bus/subscription')
8
- autoload(:Publishers, 'ribbon/event_bus/publishers')
9
- autoload(:Mixins, 'ribbon/event_bus/mixins')
9
+ autoload(:Mixins, 'ribbon/event_bus/mixins')
10
+ autoload(:Errors, 'ribbon/event_bus/errors')
10
11
 
11
12
  module_function
12
13
 
metadata CHANGED
@@ -1,15 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ribbon-event_bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.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-03 00:00:00.000000000 Z
11
+ date: 2015-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ribbon-plugins
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.2.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.2.4
13
33
  - !ruby/object:Gem::Dependency
14
34
  name: rails
15
35
  requirement: !ruby/object:Gem::Requirement
@@ -139,6 +159,9 @@ files:
139
159
  - lib/ribbon/event_bus/mixins/has_config.rb
140
160
  - lib/ribbon/event_bus/mixins/has_instance.rb
141
161
  - lib/ribbon/event_bus/mixins/serializable.rb
162
+ - lib/ribbon/event_bus/plugins.rb
163
+ - lib/ribbon/event_bus/plugins/logging_plugin.rb
164
+ - lib/ribbon/event_bus/plugins/plugin.rb
142
165
  - lib/ribbon/event_bus/publishers.rb
143
166
  - lib/ribbon/event_bus/publishers/proc_publisher.rb
144
167
  - lib/ribbon/event_bus/publishers/publisher.rb
@@ -148,7 +171,8 @@ files:
148
171
  - lib/ribbon/event_bus/subscription.rb
149
172
  - lib/ribbon/event_bus/version.rb
150
173
  homepage: http://github.com/ribbon/event_bus
151
- licenses: []
174
+ licenses:
175
+ - BSD
152
176
  metadata: {}
153
177
  post_install_message:
154
178
  rdoc_options: []