class Rails::Generators::Base

def self.hook_for(*names, &block)


end
instance.invoke controller, [ instance.name.pluralize ]
hook_for :resource_controller do |instance, controller|

can give a block to customize how the controller can be invoked.
name as the resource generator, which is singular. To change this, we
with a pluralized class name. But by default it is invoked with the same
For example, in the resource generator, the controller should be invoked

of the current class and the klass to be invoked.
going to be invoked. The block receives two arguments, an instance
You can also supply a block to hook_for to customize how the hook is

==== Custom invocations

"rails:generators:webrat", "webrat:generators:controller", "webrat"

The hooks lookup is similar as above:

rails generate controller Account --webrat

Then, if you want, webrat to be invoked, just supply:

Rails::Generators::ControllerGenerator.hook_for :webrat, :type => :boolean

This can be achieved as:
developers might want to have webrat available on controller generator.
In some cases, you want to provide a boolean hook. For example, webrat

==== Boolean hooks

rails generate controller Account --no-test-framework

Or similarly:

rails generate controller Account --skip-test-framework

to use any test framework, he can do:
All hooks come with switches for user interface. If the user don't want

==== Switches

"rails:test_unit", "test_unit:controller", "test_unit"

And the lookup is exactly the same as previously:

end
hook_for :test_framework, :in => :rails, :as => :controller
class AwesomeGenerator < Rails::Generators::Base

need to provide the :base value:
Similarly, if you want it to also lookup in the rails namespace, you just

"test_unit:controller", "test_unit"

And now it will lookup at:

end
hook_for :test_framework, :as => :controller
class AwesomeGenerator < Rails::Generators::Base

:as option:
Which is not the desired the lookup. You can change it by providing the

"test_unit:awesome", "test_unit"

The lookup in this case for test_unit as input is:

end
hook_for :test_framework
class AwesomeGenerator < Rails::Generators::Base

controller generator from test unit. Your first attempt is:
Let's suppose you are creating a generator that needs to invoke the

This can be customized with two options: :base and :as.
guessed based on class invokes hook_for, as noticed in the example above.
The first and last part used to find the generator to be invoked are

==== Options

of the hooks above.
allows any test framework to hook into Rails as long as it provides any
Rails looks for is the first and last parts of the namespace. This is what
Notice that "rails:generators:test_unit" could be loaded as well, what

"rails:test_unit", "test_unit:controller", "test_unit"

The controller generator will then try to invoke the following generators:

rails generate controller Account --test-framework=test_unit

For example, if the user invoke the controller generator as:

a generator based on the user supplied value.
The example above will create a test framework option and will invoke

end
end
hook_for :test_framework, :aliases => "-t"
class ControllerGenerator < Base
module Rails::Generators

==== Examples

is invoked and you can set a hash to customize it.
given option named "name". A class option is created when this method
Invoke a generator based on the value supplied by the user to the
def self.hook_for(*names, &block)
  options = names.extract_options!
  in_base = options.delete(:in) || base_name
  as_hook = options.delete(:as) || generator_name
  names.each do |name|
    defaults = if options[:type] == :boolean
      { }
    elsif [true, false].include?(default_value_for_option(name, options))
      { :banner => "" }
    else
      { :desc => "#{name.to_s.humanize} to be invoked", :banner => "NAME" }
    end
    unless class_options.key?(name)
      class_option(name, defaults.merge!(options))
    end
    hooks[name] = [ in_base, as_hook ]
    invoke_from_option(name, options, &block)
  end
end