Skip to content
master
Switch branches/tags
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Delegate-class

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
end

And 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
end

IMO, there are two bad things in these code:

  1. Redefine class
  2. 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
end

Now, you are delegating the instance and class methods to the wrapped class/object

Instalation

Build Status

It's is simple.

Add to your gem file

    gem "delegate/class"

And run

    bundle install

Hope you like it (:

About

Wrap a whole class (including classes methods) into a delegate class

Resources

Packages

No packages published

Languages