rails/rails — pull request #10997 by gsamokovarov (2013-06-18 18:47:03+00:00)
Title: ActiveSupport aliasing support for method delegation

I find ActiveSupport's method delegation capabilities extremely useful, however I miss the ability to delegate a method under a different name. The first thing that came into my mind was an API like the following:

delegate :config, as: :configure, to: application

While this one looks pretty nice, it breaks for the variadic arguments case. Then I taught of the Clojure destructuring and how well :as reads in there, so I came with my current API proposal:

delegate [:config, :as, :configure], to: application

This fits well with the variadic arguments case:

delegate :initialize!, :initialized?, [:config, :as, :configure], to: application

Plus, with Ruby 2 symbol array literal this one may get even easier to write:

delegate :initialize!, :initialized?, %i(config as configure), to: application

Note that this is not limited just to Ruby 2, as the arguments can be strings too, so old array of strings literals can work fine too:

delegate :initialize!, :initialized?, %w(config as configure), to: application

I am open to suggestions if you would like to propose a different syntax. Something like:

delegate [:config, :configure], to: application

may look a bit more traditional, but the ordering may be a bit confusing. I believe that with argument normalization, we can allow both forms. Your opinion, of course, can differ. Feel free to bash the idea, I just decided to hack it and throw it out there for feedback.

The current implementation is quite sloppy. Its there just as a proof of concept.