Class: TmfCommon::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/tmf_common/subscriber.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#namespaceObject (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(message, *args)
  method  = message.gsub("#{namespace}.", '')
  handler = self.class.new(namespace)
  handler.send(method, ActiveSupport::Notifications::Event.new(message, *args))
end