Project

rubyrator

0.0
No commit activity in last 3 years
No release in over 3 years
Python like method decorators for Ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
 Project Readme

Rubyrator

Python like method decorators for Ruby.

Installation

gem install rubyrator

Usage

Extend Rubyrator in a class where you want to use it, and then stick decorate :decorator_method_name before your method declaration to decorate the method.

You can set multiple decorators for your methods.

class Html
  extend Rubyrator

  def bold(dec_args, orig, *args, &block)
    "<b>" + orig.call(*args, &block) + "</b>"
  end

  def italic(dec_args, orig, *args, &block)
    "<i>" + orig.call(*args, &block) + "</i>"
  end

  decorate :bold 
  decorate :italic
  def text(text)
    text
  end
end

puts Html.new.text("decorate me please") # => "<b><i>decorate me please</i></b>"

You can add parameters to your decorator methods. Just pass them after a decorator method name, separated by the comma.

class Spamer
  extend Rubyrator

  def retry(dec_args, orig, *args, &block)
    retry_num = dec_args[0]
    retry_num.times do 
      orig.call(*args, &block)
    end
  end

  decorate :retry, 3
  def spam(spam_message)
    print spam_message
  end
end

Spamer.new.spam("spam") # => "spamspamspam"

License

Released under the MIT License. See the LICENSE file for further details.

Bitdeli Badge