跳至內容 跳至搜尋
方法
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 command 時隱藏此指令。

# 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