Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Ruby method decorators inspired by Python.

branch: master

Merge pull request #2 from bitdeli-chef/master

Add a Bitdeli Badge to README
latest commit e9a7adb6b6
Fred Wu authored November 08, 2013
Octocat-spinner-32 lib v0.0.3 September 13, 2012
Octocat-spinner-32 spec Support multiple decorators, cleaned up some code September 13, 2012
Octocat-spinner-32 .gitignore .DS_Store go away! September 12, 2012
Octocat-spinner-32 Gemfile Initial commit September 12, 2012
Octocat-spinner-32 README.md Add a Bitdeli badge to README November 09, 2013
Octocat-spinner-32 Rakefile Initial commit September 12, 2012
Octocat-spinner-32 ruby_decorators.gemspec Initial commit September 12, 2012
README.md

Ruby Decorators

Ruby method decorators inspired by Python.

I wrote this as a small practice for some ruby meta-programming fun. The implementation is relatively simple, and is thread safe.

There are also these other two implementations:

Installation

Add this line to your application's Gemfile:

gem 'ruby_decorators'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ruby_decorators

Usage

class Hi < RubyDecorator
  def call(this, *args, &blk)
    this.call(*args, &blk).sub('hello', 'hi')
  end
end

class Batman < RubyDecorator
  def call(this, *args, &blk)
    this.call(*args, &blk).sub('world', 'batman')
  end
end

class Catwoman < RubyDecorator
  def initialize(*args)
    @args = args.any? ? args : ['catwoman']
  end

  def call(this, *args, &blk)
    this.call(*args, &blk).sub('world', @args.join(' '))
  end
end

class World
  extend RubyDecorators

  def initialize
    @greeting = 'hello world'
  end

  def hello_world
    @greeting
  end

  +Batman
  def hello_batman
    @greeting
  end

  +Hi
  +Catwoman
  def hello_catwoman
    @greeting
  end

  +Catwoman.new('super', 'catwoman')
  def hello_super_catwoman
    @greeting
  end
end

world = World.new

world.hello_world          # => "hello world"
world.hello_batman         # => "hello batman"
world.hello_catwoman       # => "hi catwoman"
world.hello_super_catwoman # => "hello super catwoman"

License

Copyright (c) 2012 Fred Wu

Licensed under the MIT license.

Bitdeli Badge

Something went wrong with that request. Please try again.