Wrap a whole class (including classes methods) into a delegate class
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib/delegate
spec
.gitignore
.travis.yml
Gemfile
Guardfile
README.md
Rakefile
delegate-class.gemspec

README.md

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 (: