GithubHelp home page GithubHelp logo

method_decorators's Introduction

MethodDecorators

Python's function decorators for Ruby.

I probably wouldn't use this in production.

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.

require "method_decorators/memoize"

class MyMath
  extend MethodDecorators

  +MethodDecorators::Memoize
  def self.fib(n)
    if n <= 1
      n
    else
      fib(n - 1) + fib(n - 2)
    end
  end
end

puts MyMath.fib(200)

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.

method_decorators's People

Contributors

aef avatar gurpartap avatar inversion-des avatar kachick avatar kitaitimakoto avatar mfrost avatar michaelfairley avatar tobyhs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

method_decorators's Issues

Issues with example from README file

I had a hard time getting the fib example to work. To get an easier start with method_decorators it would be nice if that example would work right away.
The following works:

require "method_decorators/all"

class M
  extend MethodDecorators

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

puts M.new.fib(10)

This fixes these mistakes:

  • Adds the proper require line. I assumed require "method_decorators" would be adequate, and while there is some remark in the README, I overlooked that at first.
  • class Math is illegal, because Math is a module, at least in my ruby 1.9.2 installation

Multiple Prerequisites don't work

Testing out your decorators, I noticed that using multiple Precondition instances on one method causes a crash, complaining that you're calling receiver on a Proc when you call the Precondition (since the layered methods within the decorated method are all lambdas, not real methods).

I've fixed it for my needs by modifying the Decorator interface to pass self into the call chain. Maybe you know of a better fix?

It would be terrific if someone could adopt this repo

Hi michaelfairley - people get busy, so maintaining a gem can end up being a chore. This repos got a couple of pull requests and it would be nice to see them merged in or closed.

This gem's got some merit and I could see our team using it. So, I'd volunteer, but perhaps one of the pull-request submitters might like to adopt it.

Memorize Decorator

Hi,

why does the memorize decorator create a new random instance variable @memo_ivar = "@_memoize_cache#{rand(10**10)}"?

You could just create a empty cache like this:

`

def initialize
  @cache = {}
end

def call(orig, this, *args, &blk)
  return @cache[args] if @cache.has_key?(args)
  @cache[args] = orig.call(*args, &blk)
end

`

Are there any reasons?

Best regard,
Andi

Why isn't Transactional part of the gem? :)

I keep here looking for a method decorator gem specifically so I can wrap a bunch of ActiveRecord methods in transactions, only to find exactly what I'm looking for at the bottom of your README. :)

Why is the Transactional decorator merely an example, and not actually part of the gem like the other four general convenience decorator?

require 'method_decorators/decorators/memoize' does not work

Hello!

I coudn't manage how to make Memoize decorator work ('Included decorators' section in README).

Here is what I see:
stanislaw@localhost ~/work/gems/method_decorators $ irb
1.9.3-p125 :001 > require 'method_decorators'
=> true
1.9.3-p125 :002 > require 'method_decorators/decorators/memoize'
LoadError: cannot load such file -- method_decorators/decorators/memoize
...
1.9.3-p125 :003 > require 'method_decorators/decorators'
LoadError: cannot load such file -- method_decorators/decorators
...

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.