Public Instance Methods

invoke(name=nil, task=nil, args=nil, opts=nil, config=nil) click to toggle source

Receives a name and invokes it. The name can be a string (either “task” or “namespace:task”), a Thor::Task, a Class or a Thor instance. If the task cannot be guessed by name, it can also be supplied as second argument.

You can also supply the arguments, options and configuration values for the task to be invoked, if none is given, the same values used to initialize the invoker are used to initialize the invoked.


  class A < Thor
    def foo
      invoke :bar
      invoke "b:hello", ["José"]

    def bar
      invoke "b:hello", ["José"]

  class B < Thor
    def hello(name)
      puts "hello #{name}"

You can notice that the method “foo” above invokes two tasks: “bar”, which belongs to the same class and “hello” which belongs to the class B.

By using an invocation system you ensure that a task is invoked only once. In the example above, invoking “foo” will invoke “b:hello” just once, even if it’s invoked later by “bar” method.

When class A invokes class B, all arguments used on A initialization are supplied to B. This allows lazy parse of options. Let’s suppose you have some rspec tasks:

  class Rspec < Thor::Group
    class_option :mock_framework, :type => :string, :default => :rr

    def invoke_mock_framework
      invoke "rspec:#{options[:mock_framework]}"

As you noticed, it invokes the given mock framework, which might have its own options:

  class Rspec::RR < Thor::Group
    class_option :style, :type => :string, :default => :mock

Since it’s not rspec concern to parse mock framework options, when RR is invoked all options are parsed again, so RR can extract only the options that it’s going to use.

If you want Rspec::RR to be initialized with its own set of options, you have to do that explicitely:

  invoke "rspec:rr", [], :style => :foo

Besides giving an instance, you can also give a class to invoke:

  invoke Rspec::RR, [], :style => :foo
     # File lib/thor/invocation.rb, line 95
 95:     def invoke(name=nil, task=nil, args=nil, opts=nil, config=nil)
 96:       task, args, opts, config = nil, task, args, opts if task.nil? || task.is_a?(Array)
 97:       args, opts, config = nil, args, opts if args.is_a?(Hash)
 99:       object, task    = _prepare_for_invocation(name, task)
100:       klass, instance = _initialize_klass_with_initializer(object, args, opts, config)
102:       method_args = []
103:       current = @_invocations[klass]
105:       iterator = proc do |_, task|
106:         unless current.include?(
107:           current <<
108: , method_args)
109:         end
110:       end
112:       if task
113:         args ||= []
114:         method_args = args[, -1)] || []
115:, task)
116:       else
118:       end
119:     end

Disabled; run with --debug to generate this.


Generated with the Darkfish Rdoc Generator 1.1.6.