Method: Thor::Util.find_class_and_task_by_namespace

Defined in:
lib/vendor/thor/lib/thor/util.rb

.find_class_and_task_by_namespace(namespace, fallback = true) ⇒ Object

Receives a namespace and tries to retrieve a Thor or 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 < Thor
  def baz
  end
end

class Baz::Foo < Thor::Group
end

Thor::Util.namespace_to_thor_class("foo:bar")     #=> Foo::Bar, nil # will invoke default task
Thor::Util.namespace_to_thor_class("baz:foo")     #=> Baz::Foo, nil
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
# File 'lib/vendor/thor/lib/thor/util.rb', line 131

def self.find_class_and_task_by_namespace(namespace, fallback = true)
  if namespace.include?(?:) # look for a namespaced task
    pieces = namespace.split(":")
    task   = pieces.pop
    klass  = Thor::Util.find_by_namespace(pieces.join(":"))
  end
  unless klass # look for a Thor::Group with the right name
    klass, task = Thor::Util.find_by_namespace(namespace), nil
  end
  if !klass && fallback # try a task in the default namespace
    task = namespace
    klass = Thor::Util.find_by_namespace('')
  end
  return klass, task
end