Module: Ramaze::Helper::Fnordmetric::ClassMethods

Defined in:
lib/ramaze/helper/fnordmetric.rb

Overview

Holds class methods

This is used to extend the calling controller so these methods are available at the class level Since helpers are only included, extending the calling controller is done via the ‘included’ hook.

Instance Method Summary collapse

Instance Method Details

#clock(method, event_name, args = {}) ⇒ Object

This method replaces the original controller method with a times call that yields the original method. This allows to measure execution time for the method without manually modifying the method itself

Examples:

Measuring execution time for a controller action


class Users < Controller
    helper :user, :gravatar, :fnordmetric     
  ...
  def login
    ...
    # whatever login does
    ...
  end
  clock :login, :performance, :some_field => "some value"

Parameters:

  • method (Symbol)

    the method measure

  • event_name (Symbol)

    the name of the event to send to Fnordmetric.

  • args (Hash) (defaults to: {})

    a hash of supplemental data to send



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/ramaze/helper/fnordmetric.rb', line 287

def clock(method, event_name, args = {})
  # Let's alias the original controller method to original_name
  original = "__fnordmetric_%s" % method

  # We merge the method name in the args that will be sent in the event
  args.merge!(:method => "#{self.name}##{method}")

  self.class_eval do 
    # Let's create a shiny new method replacing the old one
    alias_method original, method
    private  original
    define_method(method) { |*a| times(event_name, args) do send(original, *a) end }
  end
  Ramaze::Log.debug("Clo(a)cking enabled for %s (renamed as %s)" % [ method, original ])
end