Module

Generator

Rails::Generator is a code generation platform tailored for the Rails web application framework. Generators are easily invoked within Rails applications to add and remove components such as models and controllers. New generators are easy to create and may be distributed as RubyGems, tarballs, or Rails plugins for inclusion system-wide, per-user, or per-application.

For actual examples see the rails_generator/generators directory in the Rails source (or the railties directory if you have frozen the Rails source in your application).

Generators may subclass other generators to provide variations that require little or no new logic but replace the template files.

For a RubyGem, put your generator class and templates in the lib directory. For a Rails plugin, make a generators directory at the root of your plugin.

The layout of generator files can be seen in the built-in controller generator:

generators/
  components/
    controller/
      controller_generator.rb
      templates/
        controller.rb
        functional_test.rb
        helper.rb
        view.html.erb

The directory name (controller) matches the name of the generator file (controller_generator.rb) and class (ControllerGenerator). The files that will be copied or used as templates are stored in the templates directory.

The filenames of the templates don’t matter, but choose something that will be self-explanatory since you will be referencing these in the manifest method inside your generator subclass.

Modules
Commands
Lookup Generator lookup is managed by a list of sources which return specs describing where to find and how to create generators. This module provides class methods for manipulating the source list and looking up generator specs, and an #instance wrapper for quickly instantiating generators by name.
Options
Scripts
Classes
AbstractGemSource
Base The base code generator is bare-bones. It sets up the source and destination paths and tells the logger whether to keep its trap shut.
GemGeneratorSource GemGeneratorSource hits the mines to quarry for generators. The latest versions of gems named *_generator are selected.
GemPathSource GemPathSource looks for generators within any RubyGem’s /rails_generators/<generator_name>_generator.rb file.</generator_name>
GeneratedAttribute
GeneratorError
Manifest Manifest captures the actions a generator performs. Instantiate a manifest with an optional target object, hammer it with actions, then replay or rewind on the object of your choice.
NamedBase The base generator for named components: models, controllers, mailers, etc. The target name is taken as the first argument and inflected to singular, plural, class, file, and table forms for your convenience. The remaining arguments are aliased to actions as an array for controller and mailer convenience.
PathSource PathSource looks for generators in a filesystem directory.
SimpleLogger
Source Sources enumerate (yield from #each) generator specs which describe where to find and how to create generators. Enumerable is mixed in so, for example, source.collect will retrieve every generator. Sources may be assigned a label to distinguish them.
Spec A spec knows where a generator was found and how to instantiate it. Metadata include the generator’s name, its base path, and the source which yielded it (PathSource, GemPathSource, etc.)
UsageError
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.