README.rdoc
= Method Decorator
{<img src="https://travis-ci.org/r4z3c/method_decorator.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/r4z3c/method_decorator]
{<img src="https://codeclimate.com/github/r4z3c/method_decorator/badges/gpa.svg" alt="Code Climate" />}[https://codeclimate.com/github/r4z3c/method_decorator]
{<img src="https://codeclimate.com/github/r4z3c/method_decorator/badges/coverage.svg" alt="Code Coverage" />}[https://codeclimate.com/github/r4z3c/method_decorator/coverage]
{<img src="https://codeclimate.com/github/r4z3c/method_decorator/badges/issue_count.svg" alt="Issue Count" />}[https://codeclimate.com/github/r4z3c/method_decorator]
Provides a way to dynamically override methods without losing original behavior.
== Example
For a given class:
class SomeClass
def some_method(some_arg)
puts some_arg
end
end
MethodDecorator.decorate SomeClass, :some_method do
puts "decorated_some_method_with: #{call_args}"
call_original
end
This call:
SomeClass.new.some_method 'some arg'
Produces this output:
decorated_some_method_with: ["some arg"]
some arg
== Helper Methods
Inside the block given to `MethodDecorator.decorate`, you can call four helper methods:
call_args # returns original arguments given to the call
call_block # returns original block given to the call
call_original # call the original method, with original args and block given to the call
call_original_with # call the original method, with desired args and block
== Module inclusion
You can call `decorate` method directly instead of calling `MethodDecorator.decorate`. Just include `MethodDecorator` module:
class SomeClass
def some_method(some_arg)
puts some_arg
end
include MethodDecorator
decorate :some_method do
puts "decorated_some_method_with: #{call_args}"
call_original
end
end
== Singleton Classes Support
It works too:
class SomeClass
class << self
def some_method(some_arg)
puts some_arg
end
end
end
MethodDecorator.decorate SomeClass.singleton_class, :some_method do
puts "decorated_some_method_with: #{call_args}"
call_original
end
SomeClass.some_method 'some arg'
== More
To see more of `MethodDecorator` usages, please take a look at DummyClass[https://github.com/r4z3c/method_decorator/blob/master/spec/support/dummy_class.rb].
===
{<img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" alt="Donate" />}[https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=AMPXM3PW6CTBE]