Class: Rails::Generator::Base
- Includes:
- Options
- Defined in:
- lib/rails_generator/base.rb
Overview
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. You’re probably looking for NamedBase, a subclass meant for generating “named” components such as models, controllers, and mailers.
Generators create a manifest of the actions they perform then hand the manifest to a command which replay the actions to do the heavy lifting. 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 EliteGenerator < Rails::Generator::Base
def manifest
record do |m|
m.do(some)
m.things(in) { here }
end
end
end
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#destination_root ⇒ Object
readonly
Returns the value of attribute destination_root.
-
#source_root ⇒ Object
readonly
Returns the value of attribute source_root.
Instance Method Summary collapse
-
#destination_path(relative_destination) ⇒ Object
Return the full path from the destination root for the given path.
-
#initialize(runtime_args, runtime_options = {}) ⇒ Base
constructor
A new instance of Base.
-
#manifest ⇒ Object
Generators must provide a manifest.
-
#source_path(relative_source) ⇒ Object
Return the full path from the source root for the given path.
Constructor Details
#initialize(runtime_args, runtime_options = {}) ⇒ Base
Returns a new instance of Base.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rails_generator/base.rb', line 62 def initialize(runtime_args, = {}) @args = runtime_args parse!(@args, ) # Derive source and destination paths. @source_root = [:source] || File.join(spec.path, 'templates') if [:destination] @destination_root = [:destination] elsif defined? ::RAILS_ROOT @destination_root = ::RAILS_ROOT end # Silence the logger if requested. logger.quiet = [:quiet] # Raise usage error if help is requested. usage if [:help] end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
60 61 62 |
# File 'lib/rails_generator/base.rb', line 60 def args @args end |
#destination_root ⇒ Object (readonly)
Returns the value of attribute destination_root.
60 61 62 |
# File 'lib/rails_generator/base.rb', line 60 def destination_root @destination_root end |
#source_root ⇒ Object (readonly)
Returns the value of attribute source_root.
60 61 62 |
# File 'lib/rails_generator/base.rb', line 60 def source_root @source_root end |
Instance Method Details
#destination_path(relative_destination) ⇒ Object
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'
114 115 116 |
# File 'lib/rails_generator/base.rb', line 114 def destination_path(relative_destination) File.join(destination_root, relative_destination) end |
#manifest ⇒ Object
Generators must provide a manifest. Use the record method to create a new manifest and record your generator’s actions.
83 84 85 |
# File 'lib/rails_generator/base.rb', line 83 def manifest raise NotImplementedError, "No manifest for '#{spec.name}' generator." end |
#source_path(relative_source) ⇒ Object
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'
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rails_generator/base.rb', line 95 def source_path(relative_source) # Check whether we're referring to another generator's file. name, path = relative_source.split(':', 2) # If not, return the full path to our source file. if path.nil? File.join(source_root, name) # Otherwise, ask our referral for the file. else # FIXME: this is broken, though almost always true. Others' # source_root are not necessarily the templates dir. File.join(self.class.lookup(name).path, 'templates', path) end end |