https://github.com/fredwu/ruby_decorators
Ruby method decorators inspired by Python.
https://github.com/fredwu/ruby_decorators
Last synced: 12 months ago
JSON representation
Ruby method decorators inspired by Python.
- Host: GitHub
- URL: https://github.com/fredwu/ruby_decorators
- Owner: fredwu
- Created: 2012-09-11T16:27:52.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2013-11-09T07:23:10.000Z (over 12 years ago)
- Last Synced: 2025-04-09T05:06:15.678Z (about 1 year ago)
- Language: Ruby
- Size: 132 KB
- Stars: 63
- Watchers: 3
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
- Yehuda Katz's [Ruby Decorators](https://github.com/wycats/ruby_decorators)
- Michael Fairley's [Method Decorators](https://github.com/michaelfairley/method_decorators)
## 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
```ruby
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](http://fredwu.me/)
Licensed under the [MIT license](http://fredwu.mit-license.org/).
[](https://bitdeli.com/free "Bitdeli Badge")