跳至内容 跳至搜索
方法
B
C
D
E
H
N
P
U
包含的模块

类公共方法

banner(command = nil, *)

# File railties/lib/rails/command/base.rb, line 86
def banner(command = nil, *)
  if command
    # Similar to Thor's banner, but show the namespace (minus the
    # "rails:" prefix), and show the command's declared bin instead of
    # the command runner.
    command.formatted_usage(self).gsub(/^#{namespace}:(\w+)/) { executable($1) }
  else
    executable
  end
end

base_name()

设置base_name,考虑当前类的命名空间。

Rails::Command::TestCommand.base_name # => 'rails'
# File railties/lib/rails/command/base.rb, line 106
def base_name
  @base_name ||= if base = name.to_s.split("::").first
    base.underscore
  end
end

command_name()

返回没有命名空间的命令名称。

Rails::Command::TestCommand.command_name # => 'test'
# File railties/lib/rails/command/base.rb, line 115
def command_name
  @command_name ||= if command = name.to_s.split("::").last
    command.chomp!("Command")
    command.underscore
  end
end

default_command_root()

默认文件根目录,用于放置命令可能需要的额外文件,位于命令文件上方一个文件夹。

对于放置在rails/command/test_command.rb中的Rails::Command::TestCommand,将返回rails/test

# File railties/lib/rails/command/base.rb, line 139
def default_command_root
  @default_command_root = resolve_path(".") unless defined?(@default_command_root)
  @default_command_root
end

desc(usage = nil, description = nil, options = {})

尝试从命令根目录上方一个文件夹中的USAGE文件中获取描述。

# File railties/lib/rails/command/base.rb, line 34
def desc(usage = nil, description = nil, options = {})
  if usage
    super
  else
    class_usage
  end
end

engine?()

当应用程序是 Rails 引擎时返回 true。

# File railties/lib/rails/command/base.rb, line 28
def engine?
  defined?(ENGINE_ROOT)
end

executable(command_name = self.command_name)

# File railties/lib/rails/command/base.rb, line 82
def executable(command_name = self.command_name)
  "#{bin} #{namespaced_name(command_name)}"
end

hide_command!()

便利方法,用于在运行rails命令时从可用命令中隐藏此命令。

# File railties/lib/rails/command/base.rb, line 55
def hide_command!
  Rails::Command.hidden_commands << self
end

namespace(name = nil)

便利方法,用于从类名获取命名空间。它与 Thor 默认相同,只是类名末尾的Command被删除。

# File railties/lib/rails/command/base.rb, line 45
def namespace(name = nil)
  if name
    super
  else
    @namespace ||= super.chomp("_command").sub(/:command:/, ":")
  end
end

printing_commands()

# File railties/lib/rails/command/base.rb, line 76
def printing_commands
  commands.filter_map do |name, command|
    [namespaced_name(name), command.description] unless command.hidden?
  end
end

usage_path()

在文件中查找 USAGE 描述的路径。

# File railties/lib/rails/command/base.rb, line 129
def usage_path
  @usage_path = resolve_path("USAGE") unless defined?(@usage_path)
  @usage_path
end