класс Rails::Command::Base
Публичные методы класса
# File railties/lib/rails/command/base.rb, line 77
def banner(*)
"#{executable} #{arguments.map(&:usage).join(' ')} [options]".squish
end Использовать стандартный баннер Rails.
# File railties/lib/rails/command/base.rb, line 84
def base_name
@base_name ||= begin
if base = name.to_s.split("::").first
base.underscore
end
end
end Устанавливает ::base_name, учитывая текущее пространство имён класса.
Rails::Command::TestCommand.base_name # => 'rails'
# File railties/lib/rails/command/base.rb, line 95
def command_name
@command_name ||= begin
if command = name.to_s.split("::").last
command.chomp!("Command")
command.underscore
end
end
end Возвращает имя команды без пространств имён.
Rails::Command::TestCommand.command_name # => 'test'
# File railties/lib/rails/command/base.rb, line 117
def default_command_root
path = File.expand_path(File.join("../commands", command_root_namespace), __dir__)
path if File.exist?(path)
end Базовый корень файла для размещения дополнительных файлов, необходимых команде, расположенный на уровень выше файла команды.
Для Rails::Command::TestCommand, размещённого в rails/command/test_command.rb , вернёт rails/test.
# File railties/lib/rails/command/base.rb, line 27
def desc(usage = nil, description = nil, options = {})
if usage
super
else
@desc ||= ERB.new(File.read(usage_path)).result(binding) if usage_path
end
end Попытка получить описание из файла USAGE на один уровень выше корня команды.
# File railties/lib/rails/command/base.rb, line 21 def engine? defined?(ENGINE_ROOT) end
Возвращает true, если приложение является Rails-модулем.
# File railties/lib/rails/command/base.rb, line 72
def executable
"bin/rails #{command_name}"
end # File railties/lib/rails/command/base.rb, line 48 def hide_command! Rails::Command.hidden_commands << self end
Удобный метод для скрытия этой команды из доступных при запуске rails command.
# File railties/lib/rails/command/base.rb, line 38
def namespace(name = nil)
if name
super
else
@namespace ||= super.chomp("_command").sub(/:command:/, ":")
end
end Удобный метод для получения пространства имён из имени класса. Он аналогичен поведению Thor, за исключением того, что Command в конце имени класса удаляется.
# File railties/lib/rails/command/base.rb, line 68 def printing_commands namespaced_commands end
# File railties/lib/rails/command/base.rb, line 105
def usage_path
if default_command_root
path = File.join(default_command_root, "USAGE")
path if File.exist?(path)
end
end Путь для поиска описания USAGE в файле.
Публичные методы экземпляра
# File railties/lib/rails/command/base.rb, line 148
def help
if command_name = self.class.command_name
self.class.command_help(shell, command_name)
else
super
end
end
© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.