Class: Peeek::Hook::Singleton

Inherits:
Linker
  • Object
show all
Defined in:
lib/peeek/hook/singleton.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 singleton method. return always “.”.

Returns:

  • (String)

    method prefix for singleton method. return always “.”



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

def method_prefix
  METHOD_PREFIX
end

#target_methodMethod (readonly)

Returns the method of the object.

Returns:

  • (Method)

    the method of the object



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

def target_method
  @object.method(@method_name)
end

Instance Method Details

#defined?Boolean

Determine if the method is defined in the object.

Returns:

  • (Boolean)

    whether the method is defined in the object



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

def defined?
  @object.respond_to?(@method_name, true)
end

Link the hook to the 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/singleton.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 method.

Parameters:

  • original_method (Method)

    original method



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

def unlink(original_method)
  define_method(&original_method)
end