EasyDecorator
EasyDecorator is a module that bring's in a Python-like method decorator pattern into Ruby.
Table of Contents
- Installation
- Usage
- Include Module
- Define Decorator
- Decorating Methods
- Example
- Contributing
Installation
command line
$ gem install easy_decorator
Gemfile
# ./Gemfile
source 'https://rubygems.org`
# ...
gem 'easy_decorator', '~> 0.2.1'
Usage
Include Module
# ./my_class.rb
class MyClass
include EasyDecorator
end
Define Decorator
A decorator should be defined as a method with an inner wrapper. Within the wrapper you can call the passed method via func.call(*args)
.
def my_decorator(func, *args)
wrapper do
# code here
func.call(*args)
# code here
end
end
* wrapper
is essentially syntactic sugar for a proc.
Decorating Methods
decorate(:my_decorator)
def my_method(a, b)
# code here
end
You can apply multiple decorators to a method, which will applies from last to first declared. Decorators must be declared directly above method definition.
decorate(:outer_decorator)
decorate(:inner_decorator)
def my_method(*args)
# ...
end
# is essentially the same as:
outer_decorator(inner_decorator(public_method(:my_method))).call(*args)
Example
# ./calculator.rb
class Calculator
# include module
include EasyDecorator
# define decorator
def calculate_time(func, *args)
wrap do
logger.info("Timing #{method_name}...")
start_time = Time.now
# call inner method
result = func.call(*args)
end_time = Time.now
logger.info("Processing Time: #{end_time - start_time}")
result
end
end
# decorate method
decorate(:calculate_time)
def add_numbers(a, b)
return a + b
end
end
Result:
$ Calculator.new.add_numbers(1,2)
Timing add_numbers...
Processing Time: 0.0001572930
=> 3
Contributing
- Fork the repo
- Create a your feature branch (
git checkout -b my-feature-branch
) - Update CHANGELOG.md with a bulleted list of your changes under the
unreleased
heading. - Include rspec tests for your changes
- Commit your changes to your branch (
git commit -am 'Added my feature'
) - Push to your remote forked repo (
git push origin my-featuer-branch
) - Create a new Pull Request Once I am able to review the pull request, I will either either approve and merge, or give feedback on it if I do not merge it. I will do my best to address Pull Requests as time allows.