RAA - rhook/0.1.6

rhook / 0.1.6

Short description: Easily drive AOP & hacking existing library with Ruby
Category: Library/Hacks
Status: beta
Created: 2010-11-11 02:36:32 GMT
Last update: 2010-11-26 01:03:40 GMT
Owner: Kaoru Kobo (Projects of this owner)
Homepage: http://github.com/kaorukobo/rhook
Download: http://rubygems.org/gems/rhook
License: Ruby's
Dependency:
None
Description:

Summary

  • You can provide hook point in your code,
    • and can customize its behavior from outside.
  • Also you can ‘hack’ (== injecting hook point from outside) any methods in existing code.

Example

step. Insert hook point

  require "rhook"      # <==   load library.

  class TargetBasic
    def greeting
      my_name = _rhook.does(:my_name) { "Mike"; }         # <==  my_name = "Mike"
      your_name = "Dan"
      _rhook.to.hello(your_name) + "I am #{my_name}."         # <==  hello(your_name) + "I am #{my_name}."
    end

    def hello(name)
      "Hello, #{name}. "
    end
  end #/TargetBasic

step. Add hook from outside, and change behavior.

    t = TargetBasic.new
    t._rhook.bind(:hello) { |inv|
      inv.args[0] = "Jenkins"     # change first argument.
      inv.call
    }
    t._rhook.bind(:my_name) { |inv|
      "Katherine"     # change return value.
    }

that behaves as:

    t.greeting.should == "Hello, Jenkins. I am Katherine."
                                 ~~~~~~~       ~~~~~~~~~

If you want to HACK exisitng methods, use ‘_rhook.hack’:

    t = Time.now
    t._rhook.hack(:to_s) do |inv|
      "haha! I hacked to_s."
    end
    t.to_s.should == "haha! I hacked to_s."
Versions: [0.1.6 (2010-11-26)] [0.1.5 (2010-11-15)] [0.1.2 (2010-11-11)]

Edit this project (for project owner)

back to RAA top