Class Rails::Generator::Base
In: vendor/rails/railties/lib/rails_generator/base.rb
Parent: Object

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.

Methods

Included Modules

Options

Attributes

args  [R] 
destination_root  [R] 
source_root  [R] 

Public Class methods

[Source]

     # File vendor/rails/railties/lib/rails_generator/base.rb, line 101
101:       def initialize(runtime_args, runtime_options = {})
102:         @args = runtime_args
103:         parse!(@args, runtime_options)
104: 
105:         # Derive source and destination paths.
106:         @source_root = options[:source] || File.join(spec.path, 'templates')
107:         if options[:destination]
108:           @destination_root = options[:destination]
109:         elsif defined? ::RAILS_ROOT
110:           @destination_root = ::RAILS_ROOT
111:         end
112: 
113:         # Silence the logger if requested.
114:         logger.quiet = options[:quiet]
115: 
116:         # Raise usage error if help is requested.
117:         usage if options[:help]
118:       end

Public Instance methods

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'

[Source]

     # File vendor/rails/railties/lib/rails_generator/base.rb, line 153
153:       def destination_path(relative_destination)
154:         File.join(destination_root, relative_destination)
155:       end

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

[Source]

     # File vendor/rails/railties/lib/rails_generator/base.rb, line 122
122:       def manifest
123:         raise NotImplementedError, "No manifest for '#{spec.name}' generator."
124:       end

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'

[Source]

     # File vendor/rails/railties/lib/rails_generator/base.rb, line 134
134:       def source_path(relative_source)
135:         # Check whether we're referring to another generator's file.
136:         name, path = relative_source.split(':', 2)
137: 
138:         # If not, return the full path to our source file.
139:         if path.nil?
140:           File.join(source_root, name)
141: 
142:         # Otherwise, ask our referral for the file.
143:         else
144:           # FIXME: this is broken, though almost always true.  Others'
145:           # source_root are not necessarily the templates dir.
146:           File.join(self.class.lookup(name).path, 'templates', path)
147:         end
148:       end

Protected Instance methods

Override with your own usage banner.

[Source]

     # File vendor/rails/railties/lib/rails_generator/base.rb, line 164
164:         def banner
165:           "Usage: #{$0} #{spec.name} [options]"
166:         end

Convenience method for generator subclasses to record a manifest.

[Source]

     # File vendor/rails/railties/lib/rails_generator/base.rb, line 159
159:         def record
160:           Rails::Generator::Manifest.new(self) { |m| yield m }
161:         end

Read USAGE from file in generator base path.

[Source]

     # File vendor/rails/railties/lib/rails_generator/base.rb, line 169
169:         def usage_message
170:           File.read(File.join(spec.path, 'USAGE')) rescue ''
171:         end

[Validate]