Class: TmfCommon::Subscriber
- Inherits:
-
Object
- Object
- TmfCommon::Subscriber
- Defined in:
- lib/tmf_common/subscriber.rb
Instance Attribute Summary collapse
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Class Method Summary collapse
-
.attach_to(namespace) ⇒ Object
attach public methods of subscriber with events in the namespace.
Instance Method Summary collapse
-
#call(message, *args) ⇒ Object
trigger methods when an even is captured.
-
#initialize(namespace) ⇒ Subscriber
constructor
A new instance of Subscriber.
Constructor Details
#initialize(namespace) ⇒ Subscriber
Returns a new instance of Subscriber.
5 6 7 |
# File 'lib/tmf_common/subscriber.rb', line 5 def initialize(namespace) @namespace = namespace end |
Instance Attribute Details
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
3 4 5 |
# File 'lib/tmf_common/subscriber.rb', line 3 def namespace @namespace end |
Class Method Details
.attach_to(namespace) ⇒ Object
attach public methods of subscriber with events in the namespace
10 11 12 13 14 15 |
# File 'lib/tmf_common/subscriber.rb', line 10 def self.attach_to(namespace) log_subscriber = new(namespace) log_subscriber.public_methods(false).each do |event| ActiveSupport::Notifications.subscribe("#{namespace}.#{event}", log_subscriber) end end |
Instance Method Details
#call(message, *args) ⇒ Object
trigger methods when an even is captured
18 19 20 21 22 |
# File 'lib/tmf_common/subscriber.rb', line 18 def call(, *args) method = .gsub("#{namespace}.", '') handler = self.class.new(namespace) handler.send(method, ActiveSupport::Notifications::Event.new(, *args)) end |