Module: Thor::Invocation
- Defined in:
- lib/thor/invocation.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#initialize(args = [], options = {}, config = {}, &block) ⇒ Object
Make initializer aware of invocations and the initialization args.
-
#invoke(name = nil, *args) ⇒ Object
Receives a name and invokes it.
-
#invoke_all ⇒ Object
Invoke all tasks for the current instance.
-
#invoke_task(task, *args) ⇒ Object
Invoke the given task if the given args.
-
#invoke_with_padding(*args) ⇒ Object
Invokes using shell padding.
Class Method Details
.included(base) ⇒ Object
:nodoc:
3 4 5 |
# File 'lib/thor/invocation.rb', line 3 def self.included(base) #:nodoc: base.extend ClassMethods end |
Instance Method Details
#initialize(args = [], options = {}, config = {}, &block) ⇒ Object
Make initializer aware of invocations and the initialization args.
22 23 24 25 26 |
# File 'lib/thor/invocation.rb', line 22 def initialize(args=[], ={}, config={}, &block) #:nodoc: @_invocations = config[:invocations] || Hash.new { |h,k| h[k] = [] } @_initializer = [ args, , config ] super end |
#invoke(name = nil, *args) ⇒ Object
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.
When no name is given, it will invoke the default task of the current class.
Examples
class A < Thor
def foo
invoke :bar
invoke "b:hello", ["José"]
end
def
invoke "b:hello", ["José"]
end
end
class B < Thor
def hello(name)
puts "hello #{name}"
end
end
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:#{[:mock_framework]}"
end
end
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
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 explicitely:
invoke "rspec:rr", [], :style => :foo
Besides giving an instance, you can also give a class to invoke:
invoke Rspec::RR, [], :style => :foo
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/thor/invocation.rb', line 96 def invoke(name=nil, *args) if name.nil? warn "[Thor] Calling invoke() without argument is deprecated. Please use invoke_all instead.\n#{caller.join("\n")}" return invoke_all end args.unshift(nil) if Array === args.first || NilClass === args.first task, args, opts, config = args klass, task = _retrieve_class_and_task(name, task) raise "Expected Thor class, got #{klass}" unless klass <= Thor::Base args, opts, config = (args, opts, config) klass.send(:dispatch, task, args, opts, config) end |
#invoke_all ⇒ Object
Invoke all tasks for the current instance.
123 124 125 |
# File 'lib/thor/invocation.rb', line 123 def invoke_all #:nodoc: self.class.all_tasks.map { |_, task| invoke_task(task) } end |
#invoke_task(task, *args) ⇒ Object
Invoke the given task if the given args.
113 114 115 116 117 118 119 120 |
# File 'lib/thor/invocation.rb', line 113 def invoke_task(task, *args) #:nodoc: current = @_invocations[self.class] unless current.include?(task.name) current << task.name task.run_cmd(self, *args) end end |
#invoke_with_padding(*args) ⇒ Object
Invokes using shell padding.
128 129 130 |
# File 'lib/thor/invocation.rb', line 128 def invoke_with_padding(*args) with_padding { invoke(*args) } end |