class Quails::Generators::Base

Public Class Methods

base_root() click to toggle source

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

# File railties/lib/rails/generators/base.rb, line 222
def self.base_root
  __dir__
end
default_source_root() click to toggle source

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

# File railties/lib/rails/generators/base.rb, line 213
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
desc(description = nil) click to toggle source

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

Calls superclass method
# File railties/lib/rails/generators/base.rb, line 35
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
hide!() click to toggle source

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

# File railties/lib/rails/generators/base.rb, line 55
def self.hide!
  Quails::Generators.hide_namespace(namespace)
end
hook_for(*names, &block) click to toggle source

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 Quails::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:

quails generate controller Account --test-framework=test_unit

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

"quails:test_unit", "test_unit:controller", "test_unit"

Notice that “quails:generators:test_unit” could be loaded as well, what Quails looks for is the first and last parts of the namespace. This is what allows any test framework to hook into Quails 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: :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 < Quails::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 lookup. You can change it by providing the :as option:

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

And now it will look up at:

"test_unit:controller", "test_unit"

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

class AwesomeGenerator < Quails::Generators::Base
  hook_for :test_framework, in: :quails, as: :controller
end

And the lookup is exactly the same as previously:

"quails: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:

quails generate controller Account --skip-test-framework

Or similarly:

quails 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:

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

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

quails generate controller Account --webrat

The hooks lookup is similar as above:

"quails: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 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
# File railties/lib/rails/generators/base.rb, line 168
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
namespace(name = nil) click to toggle source

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.

Calls superclass method
# File railties/lib/rails/generators/base.rb, line 48
def self.namespace(name = nil)
  return super if name
  @namespace ||= super.sub(/_generator$/, "").sub(/:generators:/, ":")
end
remove_hook_for(*names) click to toggle source

Remove a previously added hook.

remove_hook_for :orm
# File railties/lib/rails/generators/base.rb, line 194
def self.remove_hook_for(*names)
  remove_invocation(*names)

  names.each do |name|
    hooks.delete(name)
  end
end
source_root(path = nil) click to toggle source

Returns the source root for this generator using default_source_root as default.

# File railties/lib/rails/generators/base.rb, line 28
def self.source_root(path = nil)
  @_source_root = path if path
  @_source_root ||= default_source_root
end

Private Class Methods

add_shebang_option!() click to toggle source

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

# File railties/lib/rails/generators/base.rb, line 385
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
banner() click to toggle source

Use Quails default banner.

base_name() click to toggle source

Sets the base_name taking into account the current class namespace.

# File railties/lib/rails/generators/base.rb, line 319
def self.base_name # :doc:
  @base_name ||= begin
    if base = name.to_s.split("::").first
      base.underscore
    end
  end
end
default_aliases_for_option(name, options) click to toggle source

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

# File railties/lib/rails/generators/base.rb, line 346
def self.default_aliases_for_option(name, options) # :doc:
  default_for_option(Quails::Generators.aliases, name, options, options[:aliases])
end
default_for_option(config, name, options, default) click to toggle source

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

# File railties/lib/rails/generators/base.rb, line 351
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[:quails].key?(name)
    config[:quails][name]
  else
    default
  end
end
default_generator_root() click to toggle source
# File railties/lib/rails/generators/base.rb, line 411
def self.default_generator_root # :doc:
  path = File.expand_path(File.join(base_name, generator_name), base_root)
  path if File.exist?(path)
end
default_value_for_option(name, options) click to toggle source

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

# File railties/lib/rails/generators/base.rb, line 340
def self.default_value_for_option(name, options) # :doc:
  default_for_option(Quails::Generators.options, name, options, options[:default])
end
generator_name() click to toggle source

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

# File railties/lib/rails/generators/base.rb, line 329
def self.generator_name # :doc:
  @generator_name ||= begin
    if generator = name.to_s.split("::").last
      generator.sub!(/Generator$/, "")
      generator.underscore
    end
  end
end
usage_path() click to toggle source
# File railties/lib/rails/generators/base.rb, line 403
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

Private Instance Methods

class_collisions(*class_names) click to toggle source

Check whether the given class names are already taken by user application or Ruby on Quails.

# File railties/lib/rails/generators/base.rb, line 251
def class_collisions(*class_names)
  return unless behavior == :invoke

  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 Quails. Please choose an alternative and run "  \
                   "this generator again."
    end
  end
end
extract_last_module(nesting) click to toggle source

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

# File railties/lib/rails/generators/base.rb, line 272
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
indent(content, multiplier = 2) click to toggle source
# File railties/lib/rails/generators/base.rb, line 287
def indent(content, multiplier = 2) # :doc:
  spaces = " " * multiplier
  content.each_line.map { |line| line.blank? ? line : "#{spaces}#{line}" }.join
end
module_namespacing(&block) click to toggle source

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

# File railties/lib/rails/generators/base.rb, line 281
def module_namespacing(&block) # :doc:
  content = capture(&block)
  content = wrap_with_namespace(content) if namespaced?
  concat(content)
end
namespace() click to toggle source
# File railties/lib/rails/generators/base.rb, line 297
def namespace # :doc:
  Quails::Generators.namespace
end
namespace_dirs() click to toggle source
# File railties/lib/rails/generators/base.rb, line 305
def namespace_dirs
  @namespace_dirs ||= namespace.name.split("::").map(&:underscore)
end
namespaced?() click to toggle source
# File railties/lib/rails/generators/base.rb, line 301
def namespaced? # :doc:
  !options[:skip_namespace] && namespace
end
namespaced_path() click to toggle source
# File railties/lib/rails/generators/base.rb, line 309
def namespaced_path # :doc:
  @namespaced_path ||= namespace_dirs.join("/")
end
wrap_with_namespace(content) click to toggle source
# File railties/lib/rails/generators/base.rb, line 292
def wrap_with_namespace(content) # :doc:
  content = indent(content).chomp
  "module #{namespace.name}\n#{content}\nend\n"
end