Module: Skylight::Helpers::ClassMethods

Defined in:
lib/skylight/helpers.rb

Overview

See Also:

Instance Method Summary collapse

Instance Method Details

#instrument_class_method([name], opts = {}) ⇒ Object

You may also declare the methods to instrument at any time by passing the name of the method as the first argument to ‘instrument_method`.

By default, the event will be titled using the name of the class and the method. For example, in our previous example, the event name will be: MyClass.my_method. You can customize this by passing using the :title option.

Examples:

With name

class MyClass
  include Skylight::Helpers

  def self.my_method
    do_expensive_stuff
  end

  instrument_class_method :my_method
end

With title

class MyClass
  include Skylight::Helpers

  def self.my_method
    do_expensive_stuff
  end

  instrument_class_method :my_method, title: 'Expensive work'
end

Parameters:

  • [name] (Symbol|String)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :category (String) — default: 'app.method'
  • :title (String) — default: ClassName#method_name
  • :description (String)


129
130
131
132
133
134
# File 'lib/skylight/helpers.rb', line 129

def instrument_class_method(name, **opts)
  # NOTE: If the class is defined anonymously and then assigned to a variable this code
  #   will not be aware of the updated name.
  title = "#{self}.#{name}"
  __sk_instrument_method_on(__sk_singleton_class, name, title, **opts)
end

#instrument_methodObject #instrument_method([name], opts = {}) ⇒ Object

Overloads:

  • #instrument_methodObject

    Instruments the following method

    Examples:

    class MyClass
      include Skylight::Helpers
    
      instrument_method
      def my_method
        do_expensive_stuff
      end
    
    end
  • #instrument_method([name], opts = {}) ⇒ Object

    You may also declare the methods to instrument at any time by passing the name of the method as the first argument to ‘instrument_method`.

    By default, the event will be titled using the name of the class and the method. For example, in our previous example, the event name will be: MyClass#my_method. You can customize this by passing using the :title option.

    NOTE: On Ruby >= 3.2, there is an inconsistency in the order ruby2_keywords is applied when combined with instrument_method in the prefix position. We recommend not mixing these two annotations, but if you must, call ‘instrument_method` in the suffix position after your method has been defined, e.g. `instrument_method :my_method`

    Examples:

    With name

    class MyClass
      include Skylight::Helpers
    
      def my_method
        do_expensive_stuff
      end
    
      instrument_method :my_method
    
    end

    Without name

    class MyClass
      include Skylight::Helpers
    
      instrument_method title: 'Expensive work'
      def my_method
        do_expensive_stuff
      end
    end

    Parameters:

    • [name] (Symbol|String)
    • opts (Hash) (defaults to: {})

    Options Hash (opts):

    • :category (String) — default: 'app.method'
    • :title (String) — default: ClassName#method_name
    • :description (String)


85
86
87
88
89
90
91
92
# File 'lib/skylight/helpers.rb', line 85

def instrument_method(*args, **opts)
  if (name = args.pop)
    title = "#{self}##{name}"
    __sk_instrument_method_on(self, name, title, **opts)
  else
    @__sk_instrument_next_method = opts
  end
end

#method_added(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
13
14
15
16
17
18
19
# File 'lib/skylight/helpers.rb', line 12

def method_added(name)
  super

  if (opts = @__sk_instrument_next_method)
    @__sk_instrument_next_method = nil
    instrument_method(name, **opts)
  end
end

#singleton_method_added(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
25
26
27
28
29
# File 'lib/skylight/helpers.rb', line 22

def singleton_method_added(name)
  super

  if (opts = @__sk_instrument_next_method)
    @__sk_instrument_next_method = nil
    instrument_class_method(name, **opts)
  end
end