class Rails::Generators::Base

def self.add_shebang_option! # :doc:

:doc:
default value plus an instance helper method called shebang.
Small macro to add ruby as an option to the generator with proper
def self.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 :shebang do
      @shebang ||= begin
        command = if options[:ruby] == Thor::Util.ruby_command
          "/usr/bin/env #{File.basename(Thor::Util.ruby_command)}"
        else
          options[:ruby]
        end
        "#!#{command}"
      end
    end
  }
end

def self.banner # :doc:

:doc:
Use Rails default banner.
def self.banner # :doc:
  "rails generate #{namespace.delete_prefix("rails:")} #{arguments.map(&:usage).join(' ')} [options]".gsub(/\s+/, " ")
end

def self.base_name # :doc:

:doc:
Sets the base_name taking into account the current class namespace.
def self.base_name # :doc:
  @base_name ||= if base = name.to_s.split("::").first
    base.underscore
  end
end

def self.base_root

guess the default source root.
Returns the base root for a common set of generators. This is used to dynamically
def self.base_root
  __dir__
end

def self.class_option(name, options = {}) # :nodoc:

:nodoc:
Make class option aware of Rails::Generators.options and Rails::Generators.aliases.
def self.class_option(name, options = {}) # :nodoc:
  options[:desc]    = "Indicates when to generate #{name.to_s.humanize.downcase}" unless options.key?(:desc)
  options[:aliases] = default_aliases_for_option(name, options)
  options[:default] = default_value_for_option(name, options)
  super(name, options)
end

def self.default_aliases_for_option(name, options) # :doc:

:doc:
Rails::Generators.aliases.
Returns default aliases for the option name given doing a lookup in
def self.default_aliases_for_option(name, options) # :doc:
  default_for_option(Rails::Generators.aliases, name, options, options[:aliases])
end

def self.default_for_option(config, name, options, default) # :doc:

:doc:
Returns default for the option name given doing a lookup in config.
def self.default_for_option(config, name, options, default) # :doc:
  if generator_name && (c = config[generator_name.to_sym]) && c.key?(name)
    c[name]
  elsif base_name && (c = config[base_name.to_sym]) && c.key?(name)
    c[name]
  elsif config[:rails].key?(name)
    config[:rails][name]
  else
    default
  end
end

def self.default_generator_root # :doc:

:doc:
def self.default_generator_root # :doc:
  path = File.expand_path(File.join(base_name, generator_name), base_root)
  path if File.exist?(path)
end

def self.default_source_root

root, you should use source_root.
by rails to set its generators source root. If you want to customize your source
Returns the default source root for a given generator. This is used internally
def self.default_source_root
  return unless base_name && generator_name
  return unless default_generator_root
  path = File.join(default_generator_root, "templates")
  path if File.exist?(path)
end

def self.default_value_for_option(name, options) # :doc:

:doc:
Rails::Generators.options.
Returns the default value for the option name given doing a lookup in
def self.default_value_for_option(name, options) # :doc:
  default_for_option(Rails::Generators.options, name, options, options[:default])
end

def self.desc(description = nil)

root otherwise uses a default description.
Tries to get the description from a USAGE file one folder above the source
def self.desc(description = nil)
  return super if description
  @desc ||= if usage_path
    ERB.new(File.read(usage_path)).result(binding)
  else
    "Description:\n    Create #{base_name.humanize.downcase} files for #{generator_name} generator."
  end
end

def self.exit_on_failure? # :nodoc:

:nodoc:
def self.exit_on_failure? # :nodoc:
  false
end

def self.generator_name # :doc:

:doc:
Rails::Generators::ModelGenerator will return "model" as generator name.
Removes the namespaces and get the generator name. For example,
def self.generator_name # :doc:
  @generator_name ||= if generator = name.to_s.split("::").last
    generator.delete_suffix!("Generator")
    generator.underscore
  end
end

def self.hide!

running rails generator command.
Convenience method to hide this generator from the available ones when
def self.hide!
  Rails::Generators.hide_namespace(namespace)
end

def self.hook_for(*names, &block)


end
instance.invoke controller, [ instance.name.pluralize ]
hook_for :resource_controller do |instance, controller|

can give a block to customize how the controller can be invoked.
name as the resource generator, which is singular. To change this, we
with a pluralized class name. But by default it is invoked with the same
For example, in the resource generator, the controller should be invoked

of the current class and the class to be invoked.
going to be invoked. The block receives two arguments, an instance
You can also supply a block to hook_for to customize how the hook is

==== Custom invocations

"rails:generators:webrat", "webrat:generators:controller", "webrat"

The hooks lookup is similar as above:

bin/rails generate controller Account --webrat

Then, if you want webrat to be invoked, just supply:

Rails::Generators::ControllerGenerator.hook_for :webrat, type: :boolean

This can be achieved as:
developers might want to have webrat available on controller generator.
In some cases, you may want to provide a boolean hook. For example, webrat

==== Boolean hooks

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

Or similarly:

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

to use any test framework, you can do:
All hooks come with switches for user interface. If you do not want

==== Switches

"rails:test_unit", "test_unit:controller", "test_unit"

And the lookup is exactly the same as previously:

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

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

"test_unit:controller", "test_unit"

And now it will look up at:

end
hook_for :test_framework, as: :controller
class AwesomeGenerator < Rails::Generators::Base

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

"test_unit:awesome", "test_unit"

The lookup in this case for test_unit as input is:

end
hook_for :test_framework
class AwesomeGenerator < Rails::Generators::Base

controller generator from test unit. Your first attempt is:
Let's suppose you are creating a generator that needs to invoke the

This can be customized with two options: +:in+ and +:as+.
guessed based on class invokes hook_for, as noticed in the example above.
The first and last part used to find the generator to be invoked are

==== Options

of the hooks above.
allows any test framework to hook into Rails as long as it provides any
Rails looks for is the first and last parts of the namespace. This is what
Notice that "rails:generators:test_unit" could be loaded as well, what

"rails:test_unit", "test_unit:controller", "test_unit"

The controller generator will then try to invoke the following generators:

bin/rails generate controller Account --test-framework=test_unit

For example, if the user invoke the controller generator as:

a generator based on the user supplied value.
The example above will create a test framework option and will invoke

end
end
hook_for :test_framework, aliases: "-t"
class ControllerGenerator < Base
module Rails::Generators

==== Examples

is invoked and you can set a hash to customize it.
given option named "name". A class option is created when this method
Invoke a generator based on the value supplied by the user to the
def self.hook_for(*names, &block)
  options = names.extract_options!
  in_base = options.delete(:in) || base_name
  as_hook = options.delete(:as) || generator_name
  names.each do |name|
    unless class_options.key?(name)
      defaults = if options[:type] == :boolean
        {}
      elsif [true, false].include?(default_value_for_option(name, options))
        { banner: "" }
      else
        { desc: "#{name.to_s.humanize} to be invoked", banner: "NAME" }
      end
      class_option(name, defaults.merge!(options))
    end
    hooks[name] = [ in_base, as_hook ]
    invoke_from_option(name, options, &block)
  end
end

def self.hooks # :nodoc:

:nodoc:
Keep hooks configuration that are used on prepare_for_invocation.
def self.hooks # :nodoc:
  @hooks ||= from_superclass(:hooks, {})
end

def self.inherited(base) # :nodoc:

:nodoc:
source paths.
Cache source root and add lib/generators/base/generator/templates to
def self.inherited(base) # :nodoc:
  super
  # Invoke source_root so the default_source_root is set.
  base.source_root
  if base.name && !base.name.end_with?("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

def self.namespace(name = nil)

is removed.
same as Thor default except that the Generator at the end of the class
Convenience method to get the namespace from the class name. It's the
def self.namespace(name = nil)
  return super if name
  @namespace ||= super.delete_suffix("_generator").sub(/:generators:/, ":")
end

def self.prepare_for_invocation(name, value) # :nodoc:

:nodoc:
added hook is being used.
Prepare class invocation to search on Rails namespace if a previous
def self.prepare_for_invocation(name, value) # :nodoc:
  return super unless value.is_a?(String) || value.is_a?(Symbol)
  if value && constants = hooks[name]
    value = name if TrueClass === value
    Rails::Generators.find_by_namespace(value, *constants)
  elsif klass = Rails::Generators.find_by_namespace(value)
    klass
  else
    super
  end
end

def self.remove_hook_for(*names)

remove_hook_for :orm

Remove a previously added hook.
def self.remove_hook_for(*names)
  remove_invocation(*names)
  names.each do |name|
    hooks.delete(name)
  end
end

def self.source_root(path = nil)

Returns the source root for this generator using default_source_root as default.
def self.source_root(path = nil)
  @_source_root = path if path
  @_source_root ||= default_source_root
end

def self.usage_path # :doc:

:doc:
def self.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

def class_collisions(*class_names)

application or Ruby on Rails.
Check whether the given class names are already taken by user
def class_collisions(*class_names)
  return unless behavior == :invoke
  return if options.skip_collision_check?
  return if options.force?
  class_names.flatten.each do |class_name|
    class_name = class_name.to_s
    next if class_name.strip.empty?
    # Split the class from its module nesting
    nesting = class_name.split("::")
    last_name = nesting.pop
    last = extract_last_module(nesting)
    if last && last.const_defined?(last_name.camelize, false)
      raise Error, "The name '#{class_name}' is either already used in your application " \
                   "or reserved by Ruby on Rails. Please choose an alternative or use --skip-collision-check "  \
                   "or --force to skip this check and run this generator again."
    end
  end
end

def extract_last_module(nesting) # :doc:

:doc:
Takes in an array of nested modules and extracts the last module
def extract_last_module(nesting) # :doc:
  nesting.inject(Object) do |last_module, nest|
    break unless last_module.const_defined?(nest, false)
    last_module.const_get(nest)
  end
end

def indent(content, multiplier = 2) # :doc:

:doc:
def indent(content, multiplier = 2) # :doc:
  spaces = " " * multiplier
  content.each_line.map { |line| line.blank? ? line : "#{spaces}#{line}" }.join
end

def module_namespacing(&block) # :doc:

:doc:
if namespace exists and is not skipped
Wrap block with namespace of current application
def module_namespacing(&block) # :doc:
  content = capture(&block)
  content = wrap_with_namespace(content) if namespaced?
  concat(content)
end

def namespace # :doc:

:doc:
def namespace # :doc:
  Rails::Generators.namespace
end

def namespace_dirs

def namespace_dirs
  @namespace_dirs ||= namespace.name.split("::").map(&:underscore)
end

def namespaced? # :doc:

:doc:
def namespaced? # :doc:
  !options[:skip_namespace] && namespace
end

def namespaced_path # :doc:

:doc:
def namespaced_path # :doc:
  @namespaced_path ||= namespace_dirs.join("/")
end

def wrap_with_namespace(content) # :doc:

:doc:
def wrap_with_namespace(content) # :doc:
  content = indent(content).chomp
  "module #{namespace.name}\n#{content}\nend\n"
end