Movatterモバイル変換


[0]ホーム

URL:


Skip to ContentSkip to Search
Ruby on Rails 8.0.2

Class Rails::Generators::Base< Thor::Group

v8.0.2
Methods
A
B
D
E
G
H
I
M
N
R
S
U
W
Included Modules

Class Public methods

add_shebang_option!()Link

Small macro to add ruby as an option to the generator with proper default value plus an instance helper method called shebang.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 396defself.add_shebang_option!# :doc:class_option:ruby,type::string,aliases:"-r",default:Thor::Util.ruby_command,desc:"Path to the Ruby binary of your choice",banner:"PATH"no_tasks {define_method:shebangdo@shebang||=begincommand =ifoptions[:ruby]==Thor::Util.ruby_command"/usr/bin/env #{File.basename(Thor::Util.ruby_command)}"elseoptions[:ruby]end"#!#{command}"endend  }end

banner()Link

Use Rails default banner.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 329defself.banner# :doc:"bin/rails generate #{namespace.delete_prefix("rails:")} #{arguments.map(&:usage).join(' ')} [options]".gsub(/\s+/," ")end

base_name()Link

Sets thebase_name taking into account the current class namespace.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 334defself.base_name# :doc:@base_name||=ifbase =name.to_s.split("::").firstbase.underscoreendend

base_root()Link

Returns the base root for a common set of generators. This is used to dynamically guess the default source root.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 236defself.base_root__dir__end

default_aliases_for_option(name, options)Link

Returns default aliases for the option name given doing a lookup in Rails::Generators.aliases.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 357defself.default_aliases_for_option(name,options)# :doc:default_for_option(Rails::Generators.aliases,name,options,options[:aliases])end

default_for_option(config, name, options, default)Link

Returns default for the option name given doing a lookup in config.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 362defself.default_for_option(config,name,options,default)# :doc:ifgenerator_name&& (c =config[generator_name.to_sym])&&c.key?(name)c[name]elsifbase_name&& (c =config[base_name.to_sym])&&c.key?(name)c[name]elsifconfig[:rails].key?(name)config[:rails][name]elsedefaultendend

default_generator_root()Link

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 422defself.default_generator_root# :doc:path =File.expand_path(File.join(base_name,generator_name),base_root)pathifFile.exist?(path)end

default_source_root()Link

Returns the default source root for a given generator. This is used internally byRails to set its generators source root. If you want to customize your source root, you should use source_root.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 227defself.default_source_rootreturnunlessbase_name&&generator_namereturnunlessdefault_generator_rootpath =File.join(default_generator_root,"templates")pathifFile.exist?(path)end

default_value_for_option(name, options)Link

Returns the default value for the option name given doing a lookup in Rails::Generators.options.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 351defself.default_value_for_option(name,options)# :doc:default_for_option(Rails::Generators.options,name,options,options[:default])end

desc(description = nil)Link

Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 41defself.desc(description =nil)returnsuperifdescription@desc||=ifusage_pathERB.new(File.read(usage_path)).result(binding)else"Description:\n    Create #{base_name.humanize.downcase} files for #{generator_name} generator."endend

generator_name()Link

Removes the namespaces and get the generator name. For example, Rails::Generators::ModelGenerator will return “model” as generator name.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 342defself.generator_name# :doc:@generator_name||=ifgenerator =name.to_s.split("::").lastgenerator.delete_suffix!("Generator")generator.underscoreendend

hide!()Link

Convenience method to hide this generator from the available ones when running rails generator command.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 61defself.hide!Rails::Generators.hide_namespace(namespace)end

hook_for(*names, &block)Link

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"  endend

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:

$ bin/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 invokeshook_for, as noticed in the example above. This can be customized with two options::in 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_frameworkend

The lookup in this case for test_unit as input is:

"test_unit:awesome", "test_unit"

Which is not the desired lookup. You can change it by providing the:as option:

class AwesomeGenerator < Rails::Generators::Base  hook_for :test_framework, as: :controllerend

And now it will look up at:

"test_unit:controller", "test_unit"

Similarly, if you want it to also look up in the rails namespace, you just need to provide the:in value:

class AwesomeGenerator < Rails::Generators::Base  hook_for :test_framework, in: :rails, as: :controllerend

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 you do not want to use any test framework, you can do:

$ bin/rails generate controller Account --skip-test-framework

Or similarly:

$ bin/rails generate controller Account --no-test-framework

Boolean hooks

In some cases, you may 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:

$ bin/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 tohook_for to customize how the hook is going to be invoked. The block receives two arguments, an instance of the current class and the class 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

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 174defself.hook_for(*names,&block)options =names.extract_options!in_base =options.delete(:in)||base_nameas_hook =options.delete(:as)||generator_namenames.eachdo|name|unlessclass_options.key?(name)defaults =ifoptions[:type]==:boolean        {}elsif [true,false].include?(default_value_for_option(name,options))        {banner:"" }else        {desc:"#{name.to_s.humanize} to be invoked",banner:"NAME" }endclass_option(name,defaults.merge!(options))endklass =selfsingleton_class.define_method("#{name}_generator")dovalue =class_options[name].defaultRails::Generators.find_by_namespace(klass.generator_name,value)endhooks[name] = [in_base,as_hook ]invoke_from_option(name,options,&block)endend

namespace(name = nil)Link

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.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 54defself.namespace(name =nil)returnsuperifname@namespace||=super.delete_suffix("_generator").sub(/:generators:/,":")end

remove_hook_for(*names)Link

Remove a previously added hook.

remove_hook_for :orm

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 207defself.remove_hook_for(*names)remove_invocation(*names)names.eachdo|name|singleton_class.undef_method("#{name}_generator")hooks.delete(name)endend

source_root(path = nil)Link

Returns the source root for this generator usingdefault_source_root as default.

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 34defself.source_root(path =nil)@_source_root =pathifpath@_source_root||=default_source_rootend

usage_path()Link

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 414defself.usage_path# :doc:paths = [source_root&&File.expand_path("../USAGE",source_root),default_generator_root&&File.join(default_generator_root,"USAGE")  ]paths.compact.detect {|path|File.exist?path }end

Instance Private methods

extract_last_module(nesting)Link

Takes in an array of nested modules and extracts the last module

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 287defextract_last_module(nesting)# :doc:nesting.inject(Object)do|last_module,nest|breakunlesslast_module.const_defined?(nest,false)last_module.const_get(nest)endend

indent(content, multiplier = 2)Link

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 302defindent(content,multiplier =2)# :doc:spaces =" "*multipliercontent.each_line.map {|line|line.blank??line:"#{spaces}#{line}" }.joinend

module_namespacing(&block)Link

Wrap block with namespace of current application if namespace exists and is not skipped

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 296defmodule_namespacing(&block)# :doc:content =capture(&block)content =wrap_with_namespace(content)ifnamespaced?concat(content)end

namespace()Link

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 312defnamespace# :doc:Rails::Generators.namespaceend

namespaced?()Link

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 316defnamespaced?# :doc:!options[:skip_namespace]&&namespaceend

namespaced_path()Link

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 324defnamespaced_path# :doc:@namespaced_path||=namespace_dirs.join("/")end

wrap_with_namespace(content)Link

Source:show |on GitHub

# File railties/lib/rails/generators/base.rb, line 307defwrap_with_namespace(content)# :doc:content =indent(content).chomp"module #{namespace.name}\n#{content}\nend\n"end

[8]ページ先頭

©2009-2025 Movatter.jp