Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lsegal/metamorph
Transparently defines instance methods inside anonymous module so mixins can override them with proper inheritance.
https://github.com/lsegal/metamorph
Last synced: 3 months ago
JSON representation
Transparently defines instance methods inside anonymous module so mixins can override them with proper inheritance.
- Host: GitHub
- URL: https://github.com/lsegal/metamorph
- Owner: lsegal
- License: mit
- Created: 2010-03-25T18:31:29.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2010-03-26T00:42:10.000Z (almost 15 years ago)
- Last Synced: 2024-05-02T05:33:32.345Z (8 months ago)
- Language: Ruby
- Homepage:
- Size: 223 KB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Metamorph
Transparently defines instance methods inside anonymous module so mixins can
override them with proper inheritance. This allows a user to extend any class
with modules and override default behaviour without resorting to `alias_method_chain`,
even if the class defines the methods directly in the class body.## Usage
Metamorph requires no special syntax or modification to source. Simply require
the library and all methods will be transparently redefined inside inner
modules.require 'metamorph'
class A
def foo; "foo" end
end
A::InstanceMethods.instance_methods # => [:foo]
module B
def foo; super + "bar" end
end
A.new.foo # => "foo"
A.send(:include, B)
A.new.foo # => "foobar"Without `metamorph`, the second `A.new.foo` would still return "foo", because
the definition of `foo` directly in the A class body takes precedence over
any modules mixed in afterwards. Metamorph transparently changes this inheritance
rule to prioritize the "last" define method.## Installing
1. Install the gem:
sudo gem install metamorph
2. Build manually:
rake build
gem build metamorph.gemspec
sudo gem install metamorph-VERSION.gem
## Known IssuesMetamorph does not play well with codebases that perform a lot of
metaprogramming. Make sure to load metamorph *after* these libraries are
loaded.