Class: Peeek::Hook::Instance

Inherits:
Linker
  • Object
show all
Defined in:
lib/peeek/hook/instance.rb

Constant Summary collapse

METHOD_PREFIX =
'#'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Linker

inherited, #initialize

Constructor Details

This class inherits a constructor from Peeek::Hook::Linker

Instance Attribute Details

#method_prefixString (readonly)

Returns method prefix for instance method. return always “#”.

Returns:

  • (String)

    method prefix for instance method. return always “#”



10
11
12
# File 'lib/peeek/hook/instance.rb', line 10

def method_prefix
  METHOD_PREFIX
end

#target_methodUnboundMethod (readonly)

Returns the instance method of the object.

Returns:

  • (UnboundMethod)

    the instance method of the object



16
17
18
# File 'lib/peeek/hook/instance.rb', line 16

def target_method
  @object.instance_method(@method_name)
end

Instance Method Details

#defined?Boolean

Determine if the instance method is defined in the object.

Returns:

  • (Boolean)

    whether the instance method is defined in the object



23
24
25
# File 'lib/peeek/hook/instance.rb', line 23

def defined?
  @object.method_defined?(@method_name) or @object.private_method_defined?(@method_name)
end

Link the hook to the instance method.

Yields:

  • (backtrace, receiver, args)

    callback for hook

Yield Parameters:

  • backtrace (Array<String>)

    backtrace the call occurred

  • receiver (Object)

    object that received the call

  • args (Array)

    arguments at the call

Yield Returns:

  • (Object)

    return value of the original method

Raises:

  • (ArgumentError)


34
35
36
37
# File 'lib/peeek/hook/instance.rb', line 34

def link
  raise ArgumentError, 'block not supplied' unless block_given?
  define_method { |*args| yield caller, self, args }
end

Unlink the hook from the instance method.

Parameters:

  • original_method (UnboundMethod)

    original method



42
43
44
# File 'lib/peeek/hook/instance.rb', line 42

def unlink(original_method)
  define_method(original_method)
end