跳到內容 跳到搜尋
方法
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