Flowdock
method

invoke

Importance_2
Ruby latest stable (v2_5_5) - 0 notes - Class: Invocation
  • 1_8_6_287
  • 1_8_7_72
  • 1_8_7_330
  • 1_9_1_378
  • 1_9_2_180
  • 1_9_3_125
  • 1_9_3_392
  • 2_1_10
  • 2_2_9
  • 2_4_6
  • 2_5_5
  • 2_6_3 (0)
  • What's this?

Method not available on this version

This method is only available on newer versions. The first available version of the method is shown here.

invoke(name = nil, *args) public

Receives a name and invokes it. The name can be a string (either “command” or “namespace:command”), a Bundler::Thor::Command, a Class or a Bundler::Thor instance. If the command 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 command to be invoked, if none is given, the same values used to initialize the invoker are used to initialize the invoked.

When no name is given, it will invoke the default command of the current class.

Examples

class A < Bundler::Thor
  def foo
    invoke :bar
    invoke "b:hello", ["Erik"]
  end

  def bar
    invoke "b:hello", ["Erik"]
  end
end

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

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

By using an invocation system you ensure that a command 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 commands:

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

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

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

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

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 explicitly:

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

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

invoke Rspec::RR, [], :style => :foo
Show source
Register or log in to add new notes.