Method Decorator¶ ↑
<img src=“https://travis-ci.org/r4z3c/method_decorator.svg?branch=master” alt=“Build Status” /> <img src=“https://codeclimate.com/github/r4z3c/method_decorator/badges/gpa.svg” alt=“Code Climate” /> <img src=“https://codeclimate.com/github/r4z3c/method_decorator/badges/coverage.svg” alt=“Code Coverage” /> <img src=“https://codeclimate.com/github/r4z3c/method_decorator/badges/issue_count.svg” alt=“Issue Count” />
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.
¶ ↑
<img src=“https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif” alt=“Donate” />