Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Python's method decorators for Ruby

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Contributors.md
Octocat-spinner-32 Gemfile
Octocat-spinner-32 History.md
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 method_decorators.gemspec
README.md

MethodDecorators

Python's function decorators for Ruby.

Installation

gem install method_decorators

Usage

Using a decorator

Extend MethodDecorators in a class where you want to use them, and then stick +DecoratorName before your method declaration to decorate the method.

class Math
  extend MethodDecorators

  +MethodDecorators::Memoized
  def fib(n)
    if n <= 1
      1
    else
      fib(n - 1) * fib(n - 2)
    end
  end
end

You can also decorate with an instance of a decorator, rather than the class. This is useful for configuring specific options for the decorator.

class ExternalService
  extend MethodDecorators

  +MethodDecorators::Retry.new(3)
  def request
    ...
  end
end

You can also set multiple decorators for your methods. Each decorator executes within the previously declared decorator. i.e. they are nested, as expected to be.

class ExternalService
  extend MethodDecorators

  +MethodDecorators::Retry.new(3)
  +MethodDecorators::Within.new(2.seconds)
  def request
    ...
  end
end

Included decorators

Include these with require 'method_decorators/name_of_decorator', or all at once with require 'method_decorators/all'.

  • Memoize - caches the result of the method for each arg combination it's called with
  • Retry - retries the method up to n (passed in to the constructor) times if the method errors
  • Within - times outs if a request doesn't complete within n seconds
  • Precondition - raises an error if the precondition (passed as a block) is not met

Defining a decorator

class Transactional < MethodDecorators::Decorator
  def call(wrapped, this, *args, &blk)
    ActiveRecord::Base.transaction do
      wrapped.call(*args, &blk)
    end
  end
end

License

MethodDecorators is available under the MIT license and is freely available for all use, including personal, commercial, and academic. See LICENSE for details.

Something went wrong with that request. Please try again.