Class: Rails::Generators::Base
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- Rails::Generators::Base
- Includes:
- Actions, Thor::Actions
- Defined in:
- lib/rails/generators/base.rb
Direct Known Subclasses
Class Method Summary collapse
-
.base_root ⇒ Object
Returns the base root for a common set of generators.
-
.class_option(name, options = {}) ⇒ Object
Make class option aware of Rails::Generators.options and Rails::Generators.aliases.
-
.default_source_root ⇒ Object
Returns the default source root for a given generator.
-
.desc(description = nil) ⇒ Object
Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.
-
.hook_for(*names, &block) ⇒ Object
Invoke a generator based on the value supplied by the user to the given option named “name”.
-
.inherited(base) ⇒ Object
Cache source root and add lib/generators/base/generator/templates to source paths.
-
.namespace(name = nil) ⇒ Object
Convenience method to get the namespace from the class name.
-
.remove_hook_for(*names) ⇒ Object
Remove a previously added hook.
-
.source_root(path = nil) ⇒ Object
Returns the source root for this generator using default_source_root as default.
Methods included from Actions
#add_source, #capify!, #environment, #freeze!, #gem, #generate, #git, #initializer, #lib, #plugin, #rake, #rakefile, #readme, #route, #vendor
Class Method Details
.base_root ⇒ Object
Returns the base root for a common set of generators. This is used to dynamically guess the default source root.
215 216 217 |
# File 'lib/rails/generators/base.rb', line 215 def self.base_root File.dirname(__FILE__) end |
.class_option(name, options = {}) ⇒ Object
Make class option aware of Rails::Generators.options and Rails::Generators.aliases.
197 198 199 200 201 202 |
# File 'lib/rails/generators/base.rb', line 197 def self.class_option(name, ={}) #:nodoc: [:desc] = "Indicates when to generate #{name.to_s.humanize.downcase}" unless .key?(:desc) [:aliases] = default_aliases_for_option(name, ) [:default] = default_value_for_option(name, ) super(name, ) end |
.default_source_root ⇒ Object
Returns the default source root for a given generator. This is used internally by rails to set its generators source root. If you want to customize your source root, you should use source_root.
207 208 209 210 211 |
# File 'lib/rails/generators/base.rb', line 207 def self.default_source_root return unless base_name && generator_name path = File.(File.join(base_name, generator_name, 'templates'), base_root) path if File.exists?(path) end |
.desc(description = nil) ⇒ Object
Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rails/generators/base.rb', line 31 def self.desc(description=nil) return super if description usage = source_root && File.("../USAGE", source_root) @desc ||= if usage && File.exist?(usage) File.read(usage) else "Description:\n Create #{base_name.humanize.downcase} files for #{generator_name} generator." end end |
.hook_for(*names, &block) ⇒ Object
Invoke a generator based on the value supplied by the user to the given option named “name”. A class option is created when this method is invoked and you can set a hash to customize it.
Examples
module Rails::Generators
class ControllerGenerator < Base
hook_for :test_framework, :aliases => "-t"
end
end
The example above will create a test framework option and will invoke a generator based on the user supplied value.
For example, if the user invoke the controller generator as:
rails generate controller Account --test-framework=test_unit
The controller generator will then try to invoke the following generators:
"rails:test_unit", "test_unit:controller", "test_unit"
Notice that “rails:generators:test_unit” could be loaded as well, what Rails looks for is the first and last parts of the namespace. This is what allows any test framework to hook into Rails as long as it provides any of the hooks above.
Options
The first and last part used to find the generator to be invoked are guessed based on class invokes hook_for, as noticed in the example above. This can be customized with two options: :base and :as.
Let’s suppose you are creating a generator that needs to invoke the controller generator from test unit. Your first attempt is:
class AwesomeGenerator < Rails::Generators::Base
hook_for :test_framework
end
The lookup in this case for test_unit as input is:
"test_unit:awesome", "test_unit"
Which is not the desired the lookup. You can change it by providing the :as option:
class AwesomeGenerator < Rails::Generators::Base
hook_for :test_framework, :as => :controller
end
And now it will lookup at:
"test_unit:controller", "test_unit"
Similarly, if you want it to also lookup in the rails namespace, you just need to provide the :base value:
class AwesomeGenerator < Rails::Generators::Base
hook_for :test_framework, :in => :rails, :as => :controller
end
And the lookup is exactly the same as previously:
"rails:test_unit", "test_unit:controller", "test_unit"
Switches
All hooks come with switches for user interface. If the user don’t want to use any test framework, he can do:
rails generate controller Account --skip-test-framework
Or similarly:
rails generate controller Account --no-test-framework
Boolean hooks
In some cases, you want to provide a boolean hook. For example, webrat developers might want to have webrat available on controller generator. This can be achieved as:
Rails::Generators::ControllerGenerator.hook_for :webrat, :type => :boolean
Then, if you want, webrat to be invoked, just supply:
rails generate controller Account --webrat
The hooks lookup is similar as above:
"rails:generators:webrat", "webrat:generators:controller", "webrat"
Custom invocations
You can also supply a block to hook_for to customize how the hook is going to be invoked. The block receives two arguments, an instance of the current class and the klass to be invoked.
For example, in the resource generator, the controller should be invoked with a pluralized class name. But by default it is invoked with the same name as the resource generator, which is singular. To change this, we can give a block to customize how the controller can be invoked.
hook_for :resource_controller do |instance, controller|
instance.invoke controller, [ instance.name.pluralize ]
end
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rails/generators/base.rb', line 159 def self.hook_for(*names, &block) = names. in_base = .delete(:in) || base_name as_hook = .delete(:as) || generator_name names.each do |name| defaults = if [:type] == :boolean { } elsif [true, false].include?(default_value_for_option(name, )) { :banner => "" } else { :desc => "#{name.to_s.humanize} to be invoked", :banner => "NAME" } end unless .key?(name) class_option(name, defaults.merge!()) end hooks[name] = [ in_base, as_hook ] invoke_from_option(name, , &block) end end |
.inherited(base) ⇒ Object
Cache source root and add lib/generators/base/generator/templates to source paths.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/rails/generators/base.rb', line 221 def self.inherited(base) #:nodoc: super # Invoke source_root so the default_source_root is set. base.source_root if base.name && base.name !~ /Base$/ Rails::Generators.subclasses << base Rails::Generators.templates_path.each do |path| if base.name.include?('::') base.source_paths << File.join(path, base.base_name, base.generator_name) else base.source_paths << File.join(path, base.generator_name) end end end end |
.namespace(name = nil) ⇒ Object
Convenience method to get the namespace from the class name. It’s the same as Thor default except that the Generator at the end of the class is removed.
45 46 47 48 |
# File 'lib/rails/generators/base.rb', line 45 def self.namespace(name=nil) return super if name @namespace ||= super.sub(/_generator$/, '').sub(/:generators:/, ':') end |
.remove_hook_for(*names) ⇒ Object
Remove a previously added hook.
Examples
remove_hook_for :orm
188 189 190 191 192 193 194 |
# File 'lib/rails/generators/base.rb', line 188 def self.remove_hook_for(*names) remove_invocation(*names) names.each do |name| hooks.delete(name) end end |
.source_root(path = nil) ⇒ Object
Returns the source root for this generator using default_source_root as default.
24 25 26 27 |
# File 'lib/rails/generators/base.rb', line 24 def self.source_root(path=nil) @_source_root = path if path @_source_root ||= default_source_root end |