Kumadori
This is simple decorator for Ruby.
Kumadori mean make-up for Japanese Kabuki.
Installation
Add this line to your application's Gemfile:
gem 'kumadori'
And then execute:
$ bundle
Or install it yourself as:
$ gem install kumadori
Usage
Kumadori decorate your instance by #{instance.class}Decorator class.
#
# Basic ruby class.
#
class User
attr_accesstor :first_name, :last_name
def initialize(first_name, last_name)
self.first_name = first_name
self.last_name = last_name
end
end
#
# Decorator class for User instance.
#
class UserDecorator < Kumadori::BaseDecorator
def full_name
"#{self.last_name} #{self.first_name}"
end
end
user = User.new('Yuji', 'Arakaki')
# user instance decorated by UserDecorator
decorated_user = Kumadori.decorate(user)
decorated_user.full_name # => "Arakaki Yuji"
if you not defined #{instance.class}Decorator class, it's instance decorated by Kumadori::BaseDecorator. So, if you want defined method for every instance, you just override Kumadori::BaseDecorator, and defined method.
class Animal
end
module Kumadori
class BaseDecorator < ::SimpleDelegator
def live?
true
end
end
end
animal = Animal.new
# Because of AnimalDecorator class is not defiend,
# it is decorated by Kumadori::BaseDecorator
decorated_animal = Kumadori.decorate(animal)
decorated_animal.live? # => true
if you want to decorate all items in collection, use Kumadori.collection_decorate method.
members = []
members << User.new('Kanoko', 'Higa')
members << User.new('Ai', 'Kawasaki')
members << User.new('Takeo', 'Kikuchi')
decorated_members = Kumadori.collection_decorate(members)
decorated_members.map{ |user| user.full_name} # => ["Higa Kanoko", "Kawasaki Ai", "Kikuchi Takeo"]
Contributing
- Fork it ( https://github.com/Arakaki-Yuji/kumadori/fork )
- 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 a new Pull Request