Class: AppMap::Hook::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/appmap/hook/method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hook_class, hook_method) ⇒ Method

Returns a new instance of Method.



9
10
11
12
13
14
# File 'lib/appmap/hook/method.rb', line 9

def initialize(hook_class, hook_method)
  @hook_class = hook_class
  @hook_method = hook_method
  @defined_class, method_symbol = Hook.qualify_method_name(@hook_method)
  @method_display_name = [@defined_class, method_symbol, @hook_method.name].join
end

Instance Attribute Details

#defined_classObject (readonly)

Returns the value of attribute defined_class.



4
5
6
# File 'lib/appmap/hook/method.rb', line 4

def defined_class
  @defined_class
end

#hook_classObject (readonly)

Returns the value of attribute hook_class.



4
5
6
# File 'lib/appmap/hook/method.rb', line 4

def hook_class
  @hook_class
end

#hook_methodObject (readonly)

Returns the value of attribute hook_method.



4
5
6
# File 'lib/appmap/hook/method.rb', line 4

def hook_method
  @hook_method
end

#method_display_nameObject (readonly)

Returns the value of attribute method_display_name.



4
5
6
# File 'lib/appmap/hook/method.rb', line 4

def method_display_name
  @method_display_name
end

Instance Method Details

#activateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/appmap/hook/method.rb', line 16

def activate
  warn "AppMap: Hooking #{method_display_name}" if Hook::LOG

  hook_method = self.hook_method
  before_hook = self.method(:before_hook)
  after_hook = self.method(:after_hook)
  with_disabled_hook = self.method(:with_disabled_hook)

  hook_class.define_method hook_method.name do |*args, &block|
    instance_method = hook_method.bind(self).to_proc

    hook_disabled = Thread.current[HOOK_DISABLE_KEY]
    enabled = true if !hook_disabled && AppMap.tracing.enabled?
    return instance_method.call(*args, &block) unless enabled

    call_event, start_time = with_disabled_hook.() do
      before_hook.(self, args)
    end
    return_value = nil
    exception = nil
    begin
      return_value = instance_method.(*args, &block)
    rescue
      exception = $ERROR_INFO
      raise
    ensure
      with_disabled_hook.() do
        after_hook.(call_event, start_time, return_value, exception)
      end
    end
  end
end