Class: Object

Inherits:
BasicObject
Defined in:
lib/rallhook.rb

Overview

To facilitate the use of hooking/redirection features, rallhook defines some methods in the Object class

Instance Method Summary collapse

Instance Method Details

#redirect(method_name, klass = nil) ⇒ Object

Generates a redirect message to return it in handle_method to cause the redirection of the method that is beign processed with the object as receiver, the method_name the new target and the class as second parameter if specified

Example:

class X
  def foo(*args)
  end
end

class MethodHandler
  @@x = x.new
  def handle_method(klass, recv, m , method_id)
    if m == :bar then
      # redirect bar calls to foo in x
      return x.redirect(:foo)
    end
    nil # do nothing
  end
end

# hook using MethodHandler, etc... (see README and examples)


362
363
364
365
366
367
368
# File 'lib/rallhook.rb', line 362

def redirect(method_name, klass = nil)
if klass
  ::RallHook::Redirect.new(klass,self,method_name)
else
  ::RallHook::Redirect.new(self.class,self,method_name)
end
end

#redirect_with_unhook(method_name, klass = nil) ⇒ Object

Same as redirect, but disable the hook/interception of methods (dont call handle_redirect) before make the call, the hook status may be restored with Hook#rehook Anyway, it is desirable to use MethodWrapper instead to “wrap” method calls

see RallHook::Helper::MethodWrapper



329
330
331
332
333
334
335
336
# File 'lib/rallhook.rb', line 329

def redirect_with_unhook(method_name, klass = nil)
  if klass
    ::RallHook::Redirect.new(klass,self,method_name,true)
  else
    ::RallHook::Redirect.new(self.class,self,method_name,true)
  end

end

#specific_method(arg1, arg2 = nil) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/rallhook.rb', line 370

def specific_method(arg1, arg2=nil)
  if arg2
    method_name = arg2
    klass = arg1

    if instance_of? Class
      method(method_name)
    else
      klass.shadow.instance_method(method_name).unchecked_bind(self)
    end
  else
    method(arg1)
  end
end