Using the default ruby delegate library, this gem is used to wrap the class AND instance methods of another class. (Though I still don't know the real usage of it)
You may want to use it like this:
##Usage
I found it can be used on these situations.
Lets say you are using Rails and has the model:
class User < ActiveRecord::Base
endAnd then you have an author delegator class, modified to make things like #form_for
require "delegate"
class Author < SimpleDelegator
def class
__getojb__.class
end
def bookmates
User.find(1)
end
def name
super + "!"
end
endIMO, there are two bad things in these code:
- Redefine class
- Referencing the class you should be delegating
You may change it to:
require "delegate/class"
class Author < ClassDelegator(User)
def bookmates
self.class.find(1)
end
def name
super + "!"
end
endNow, you are delegating the instance and class methods to the wrapped class/object
It's is simple.
Add to your gem file
gem "delegate/class"And run
bundle installHope you like it (:
