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
- Host: GitHub
- URL: https://github.com/zimbatm/methodchain
- Owner: zimbatm
- Created: 2009-06-02T17:55:23.000Z (about 17 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T05:30:55.000Z (over 2 years ago)
- Last Synced: 2025-07-09T07:52:21.912Z (12 months ago)
- Language: Ruby
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.rdoc
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.