Method: Bundler::Thor::Util.find_class_and_command_by_namespace
- Defined in:
- lib/bundler/vendor/thor/lib/thor/util.rb
.find_class_and_command_by_namespace(namespace, fallback = true) ⇒ Object Also known as: find_class_and_task_by_namespace
Receives a namespace and tries to retrieve a Bundler::Thor or Bundler::Thor::Group class from it. It first searches for a class using the all the given namespace, if it’s not found, removes the highest entry and searches for the class again. If found, returns the highest entry as the class name.
Examples
class Foo::Bar < Bundler::Thor
def baz
end
end
class Baz::Foo < Bundler::Thor::Group
end
Bundler::Thor::Util.namespace_to_thor_class("foo:bar") #=> Foo::Bar, nil # will invoke default command
Bundler::Thor::Util.namespace_to_thor_class("baz:foo") #=> Baz::Foo, nil
Bundler::Thor::Util.namespace_to_thor_class("foo:bar:baz") #=> Foo::Bar, "baz"
Parameters
namespace<String>
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/bundler/vendor/thor/lib/thor/util.rb', line 131 def find_class_and_command_by_namespace(namespace, fallback = true) if namespace.include?(":") # look for a namespaced command *pieces, command = namespace.split(":") namespace = pieces.join(":") namespace = "default" if namespace.empty? klass = Bundler::Thor::Base.subclasses.detect { |thor| thor.namespace == namespace && thor.command_exists?(command) } end unless klass # look for a Bundler::Thor::Group with the right name klass = Bundler::Thor::Util.find_by_namespace(namespace) command = nil end if !klass && fallback # try a command in the default namespace command = namespace klass = Bundler::Thor::Util.find_by_namespace("") end [klass, command] end |