Module: Neo4j::Core::Instrumentable::ClassMethods

Defined in:
lib/neo4j/core/instrumentable.rb

Instance Method Summary collapse

Instance Method Details

#instrument(name, label, arguments) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/neo4j/core/instrumentable.rb', line 15

def instrument(name, label, arguments)
  # defining class methods
  klass = class << self; self; end
  klass.instance_eval do
    define_method("subscribe_to_#{name}") do |&b|
      ActiveSupport::Notifications.subscribe(label) do |a, start, finish, id, payload|
        b.call yield(a, start, finish, id, payload)
      end
    end

    define_method("instrument_#{name}") do |*args, &b|
      hash = arguments.each_with_index.each_with_object({}) do |(argument, i), result|
        result[argument.to_sym] = args[i]
      end
      ActiveSupport::Notifications.instrument(label, hash) { b.call }
    end
  end
end