An open API service indexing awesome lists of open source software.

https://github.com/zimbatm/methodchain

Replace methods in an existing class but keep super
https://github.com/zimbatm/methodchain

Last synced: 11 months ago
JSON representation

Replace methods in an existing class but keep super

Awesome Lists containing this project

README

          

= MethodChain

MethodChain#chain_method is a replacement of alias_method_chain

Instead of aliasing the method, it moves the original method in a module
and includes it in the class. That way, you can still use the super keyworkd.

If you look at your class ancestors, you can see the #
instance with the method name, the reason if it was given and where it
was called. That way, it is easy to keep track at runtime what methods
where replaced and where.

Extend your object with this module to add the MethodChain#chain_method method
(see method definition for more doc)

== Usage

require 'methodchain'

class MyClass
extend MethodChain
def mymeth; "hello" end
chain_method :mymeth, "replace original"
def mymeth; super + "world" end
end
MyClass.new.mymeth #=> "helloworld"
MyClass.ancestors
#=> [MyClass,
# #,
# Object, Kernel]

== Features

* 100% test coverage
* One file implementation, easy to put in your project
* Lots of meta-guru magic

== Project infos

Homepage:: http://github.com/zimbatm/methodchain
Licence:: public domain

== Stability & bug report

The current version has 100% test coverage. However, it was not used in
lots of real-world projects. If you find an error, report to
http://github.com/zimbatm/methodchain

== Ruby 1.9 notes

This current implementation does not work with ruby 1.9 because define_method
now uses Method#bind, which does not allow binding to other classes.

Next version will probably implement a bouncer module to intercept super calls
and bind the methods to the current class. That way, it should work in ruby1.9
without hacking the internals. It should also allow re-ordering the call order.