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

类公共方法

banner(command = nil, *)

# File railties/lib/rails/command/base.rb, line 84
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 104
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 113
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 137
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 32
def desc(usage = nil, description = nil, options = {})
  if usage
    super
  else
    class_usage
  end
end

engine?()

当应用是 Rails engine 时返回 true。

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

executable(command_name = self.command_name)

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

hide_command!()

当运行 rails 命令时,用于从可用命令中隐藏此命令的便捷方法。

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

namespace(name = nil)

从类名获取命名空间的便捷方法。它与 Thor 的默认值相同,只是移除了类名末尾的 Command

# File railties/lib/rails/command/base.rb, line 43
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 74
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 127
def usage_path
  @usage_path = resolve_path("USAGE") unless defined?(@usage_path)
  @usage_path
end