Module: Instrumentable::ClassMethods

Defined in:
lib/instrumentable.rb

Instance Method Summary collapse

Instance Method Details

#instrument_for(method_to_instrument, event_name, payload = {}) ⇒ Object

Internal: Decorates :method_to_instrument with AS::N.instrument firing with :event_name to the matching AS::N.subscribe

Example:

# Decorates render method with AS:N:instrument 'model.render' and passes
# a payload of :model_name and :id to the subscribe method
instrument_for :render, 'model.render', {:model_name => :model_name, :id => :id


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/instrumentable.rb', line 21

def instrument_for(method_to_instrument, event_name, payload={})
  instrument_method = :"instrument_for_#{method_to_instrument}"

  # Hide original method under new method
  alias_method instrument_method, method_to_instrument

  # Redefine method_to_instrument to call inside the Notification
  define_method(method_to_instrument) do |*args, &block|
    callable_payload = payload.inject({}) do |result, element|
      value = (__send__(element.last) if respond_to?(element.last)) || element.last
      result.tap { |r| r[element.first] = value }
    end
    ActiveSupport::Notifications.instrument event_name, callable_payload do
      __send__(instrument_method, *args, &block)
    end
  end
end