Class

Rails::Generator::Base

Inheritance
< Object
Included Modules
Options

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.

It‘s useful for copying files such as stylesheets, images, or javascripts.

For more comprehensive template-based passive code generation with arguments, you‘ll want Rails::Generator::NamedBase.

Generators create a manifest of the actions they perform then hand the manifest to a command which replays the actions to do the heavy lifting (such as checking for existing files or creating directories if needed). Create, destroy, and list commands are included. Since a single manifest may be used by any command, creating new generators is as simple as writing some code templates and declaring what you‘d like to do with them.

The manifest method must be implemented by subclasses, returning a Rails::Generator::Manifest. The record method is provided as a convenience for manifest creation. Example:

  class StylesheetGenerator < Rails::Generator::Base
    def manifest
      record do |m|
        m.directory('public/stylesheets')
        m.file('application.css', 'public/stylesheets/application.css')
      end
    end
  end

See Rails::Generator::Commands::Create for a list of methods available to the manifest.

Attributes

Name Visibility R/W Description
args public R
destination_root public R
source_root public R

Methods

Class

Visibility Signature
public new (runtime_args, runtime_options = {})

Instance

Visibility Signature
public after_generate ()
public destination_path (relative_destination)
public manifest ()
public source_path (relative_source)
protected banner ()
protected record () {|m| ...}
protected usage_message ()

Class Method Detail

new(runtime_args, runtime_options = {})

Instance Method Detail

after_generate()

destination_path(relative_destination)

Return the full path from the destination root for the given path. Example for destination_root = ’/dest’:

  destination_path('some/path.rb') == '/dest/some/path.rb'

manifest()

Generators must provide a manifest. Use the record method to create a new manifest and record your generator‘s actions.

source_path(relative_source)

Return the full path from the source root for the given path. Example for source_root = ’/source’:

  source_path('some/path.rb') == '/source/some/path.rb'

The given path may include a colon ’:’ character to indicate that the file belongs to another generator. This notation allows any generator to borrow files from another. Example:

  source_path('model:fixture.yml') = '/model/source/path/fixture.yml'

banner()

Override with your own usage banner.

record() {|m| ...}

Convenience method for generator subclasses to record a manifest.

usage_message()

Read USAGE from file in generator base path.