0.0
No commit activity in last 3 years
No release in over 3 years
Bring in Python's decorator pattern for Ruby methods.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

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

  1. Fork the repo
  2. Create a your feature branch (git checkout -b my-feature-branch)
  3. Update CHANGELOG.md with a bulleted list of your changes under the unreleased heading.
  4. Include rspec tests for your changes
  5. Commit your changes to your branch (git commit -am 'Added my feature')
  6. Push to your remote forked repo (git push origin my-featuer-branch)
  7. 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.