класс Rails::Command::Base
Публичные методы класса
# File railties/lib/rails/command/base.rb, line 99
def banner(*)
"#{executable} #{arguments.map(&:usage).join(' ')} [options]".squish
end Использование стандартного баннера 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 Устанавливает base_name с учётом текущего пространства имён класса.
Rails::Command::TestCommand.base_name # => 'rails'
# 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 Возвращает имя команды без пространств имён.
Rails::Command::TestCommand.command_name # => 'test'
# File railties/lib/rails/command/base.rb, line 135 def default_command_root path = File.expand_path(relative_command_path, __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 49
def desc(usage = nil, description = nil, options = {})
if usage
super
else
@desc ||= ERB.new(File.read(usage_path), trim_mode: "-").result(binding) if usage_path
end
end Попытка получить описание из файла USAGE на уровне выше корневой папки команды.
# File railties/lib/rails/command/base.rb, line 43 def engine? defined?(ENGINE_ROOT) end
Возвращает true, если приложение является Rails движком.
# File railties/lib/rails/command/base.rb, line 94
def executable
"rails #{command_name}"
end # File railties/lib/rails/command/base.rb, line 70 def hide_command! Rails::Command.hidden_commands << self end
Удобный метод для скрытия этой команды из доступных при выполнении rails command.
# File railties/lib/rails/command/base.rb, line 60
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 90 def printing_commands namespaced_commands end
# File railties/lib/rails/command/base.rb, line 123
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 174
def help
if command_name = self.class.command_name
self.class.command_help(shell, command_name)
else
super
end
end
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.