PlasticWrap
A decorator.
Installation
Add this line to your application's Gemfile:
gem 'plastic_wrap'
And then execute:
$ bundle
Or install it yourself as:
$ gem install plastic_wrap
Usage
PlasticWrap is easy to use. There are 2 variants of wrapping.
The easiest and most maleable is Light. This uses SimpleDelegator under the covers to do all the heavy lifting.
class Leftovers
def best_before
@best_before ||= Time.now
end
end
class ReadableBestBefore < PlasticWrap::Light
def best_before
super.strftime('%m-%y')
end
end
ReadableBestBefore.wrap(Leftovers.new).best_before #=> 1-2001The other variant is Heavy. This uses Delegator under the covers for its lifting. You may want to use this is you don't want to deal with tracing through method_missing calls.
class Leftovers
def best_before
@best_before ||= Time.now
end
end
class ReadableBestBefore < PlasticWrap.create_wrap(Leftovers)
def best_before
super.strftime('%m-%y')
end
end
ReadableBestBefore.wrap(Leftovers.new).best_before #=> 1-2001No matter which variant you decide to use; the wrap method accepts Enumerables and will return an array of nicely wrapped models. You also get the benefit of all the built-in Rails helpers in your decorators.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request