123456789_123456789_123456789_123456789_123456789_

Module: Bundler::Thor::Invocation

Relationships & Source Files
Namespace Children
Modules:
Defined in: lib/bundler/vendor/thor/lib/thor/invocation.rb

Class Method Summary

Instance Method Summary

Class Method Details

.included(base)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 3

def self.included(base) #:nodoc:
  base.extend ClassMethods
end

Instance Method Details

#_parse_initialization_options(args, opts, config) (protected)

This method is for internal use only.

Initialize klass using values stored in the @_initializer.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 165

def _parse_initialization_options(args, opts, config) #:nodoc:
  stored_args, stored_opts, stored_config = @_initializer

  args ||= stored_args.dup
  opts ||= stored_opts.dup

  config ||= {}
  config = stored_config.merge(_shared_configuration).merge!(config)

  [args, opts, config]
end

#_retrieve_class_and_command(name, sent_command = nil) (protected) Also known as: #_retrieve_class_and_task

This method is for internal use only.

This method simply retrieves the class and command to be invoked. If the name is nil or the given name is a command in the current class, use the given name and return self as class. Otherwise, call prepare_for_invocation in the current class.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 152

def _retrieve_class_and_command(name, sent_command = nil) #:nodoc:
  if name.nil?
    [self.class, nil]
  elsif self.class.all_commands[name.to_s]
    [self.class, name.to_s]
  else
    klass, command = self.class.prepare_for_invocation(nil, name)
    [klass, command || sent_command]
  end
end

#_retrieve_class_and_task(name, sent_command = nil) (protected)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 162

alias_method :_retrieve_class_and_task, :_retrieve_class_and_command

#_shared_configuration (protected)

This method is for internal use only.

Configuration values that are shared between invocations.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 144

def _shared_configuration #:nodoc:
  {:invocations => @_invocations}
end

#current_command_chain

Make the current command chain accessible with in a Bundler::Thor-(sub)command

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 29

def current_command_chain
  @_invocations.values.flatten.map(&:to_sym)
end

#initialize(args = [], options = {}, config = {}, &block)

This method is for internal use only.

Make initializer aware of invocations and the initialization args.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 22

def initialize(args = [], options = {}, config = {}, &block) #:nodoc:
  @_invocations = config[:invocations] || Hash.new { |h, k| h[k] = [] }
  @_initializer = [args, options, config]
  super
end

#invoke(name = nil, *args)

Receives a name and invokes it. The name can be a string (either “command” or “namespace:command”), a 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
[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 101

def invoke(name = nil, *args)
  if name.nil?
    warn "[Bundler::Thor] Calling invoke() without argument is deprecated. Please use invoke_all instead.\n#{caller.join("\n")}"
    return invoke_all
  end

  args.unshift(nil) if args.first.is_a?(Array) || args.first.nil?
  command, args, opts, config = args

  klass, command = _retrieve_class_and_command(name, command)
  raise "Missing Bundler::Thor class for invoke #{name}" unless klass
  raise "Expected Bundler::Thor class, got #{klass}" unless klass <= Bundler::Thor::Base

  args, opts, config = _parse_initialization_options(args, opts, config)
  klass.send(:dispatch, command, args, opts, config) do |instance|
    instance.parent_options = options
  end
end

#invoke_all

This method is for internal use only.

Invoke all commands for the current instance.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 132

def invoke_all #:nodoc:
  self.class.all_commands.map { |_, command| invoke_command(command) }
end

#invoke_command(command, *args) Also known as: #invoke_task

This method is for internal use only.

Invoke the given command if the given args.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 121

def invoke_command(command, *args) #:nodoc:
  current = @_invocations[self.class]

  unless current.include?(command.name)
    current << command.name
    command.run(self, *args)
  end
end

#invoke_task(command, *args)

Alias for #invoke_command.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 129

alias_method :invoke_task, :invoke_command

#invoke_with_padding(*args)

Invokes using shell padding.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/invocation.rb', line 137

def invoke_with_padding(*args)
  with_padding { invoke(*args) }
end