lib/thor/invocation.rb in thor-0.18.1 vs lib/thor/invocation.rb in thor-0.19.0

- old
+ new

@@ -17,16 +17,21 @@ end end end # Make initializer aware of invocations and the initialization args. - def initialize(args=[], options={}, config={}, &block) #:nodoc: - @_invocations = config[:invocations] || Hash.new { |h,k| h[k] = [] } - @_initializer = [ args, options, config ] + def initialize(args = [], options = {}, config = {}, &block) #:nodoc: + @_invocations = config[:invocations] || Hash.new { |h, k| h[k] = [] } + @_initializer = [args, options, config] super end + # Make the current command chain accessible with in a Thor-(sub)command + def current_command_chain + @_invocations.values.flatten.map(&:to_sym) + end + # Receives a name and invokes it. The name can be a string (either "command" or # "namespace:command"), a Thor::Command, a Class or a 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 @@ -38,15 +43,15 @@ # ==== Examples # # class A < Thor # def foo # invoke :bar - # invoke "b:hello", ["José"] + # invoke "b:hello", ["Erik"] # end # # def bar - # invoke "b:hello", ["José"] + # invoke "b:hello", ["Erik"] # end # end # # class B < Thor # def hello(name) @@ -91,21 +96,22 @@ # # Besides giving an instance, you can also give a class to invoke: # # invoke Rspec::RR, [], :style => :foo # - def invoke(name=nil, *args) + 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 + 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 "Expected Thor class, got #{klass}" unless klass <= Thor::Base + fail "Missing Thor class for invoke #{name}" unless klass + fail "Expected Thor class, got #{klass}" unless klass <= 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 @@ -118,11 +124,11 @@ unless current.include?(command.name) current << command.name command.run(self, *args) end end - alias invoke_task invoke_command + alias_method :invoke_task, :invoke_command # Invoke all commands for the current instance. def invoke_all #:nodoc: self.class.all_commands.map { |_, command| invoke_command(command) } end @@ -130,43 +136,43 @@ # Invokes using shell padding. def invoke_with_padding(*args) with_padding { invoke(*args) } end - protected + protected - # Configuration values that are shared between invocations. - def _shared_configuration #:nodoc: - { :invocations => @_invocations } - end + # Configuration values that are shared between invocations. + def _shared_configuration #:nodoc: + {:invocations => @_invocations} + end - # 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. - def _retrieve_class_and_command(name, sent_command=nil) #:nodoc: - case - when name.nil? - [self.class, nil] - when 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 + # 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. + def _retrieve_class_and_command(name, sent_command = nil) #:nodoc: + case + when name.nil? + [self.class, nil] + when 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 - alias _retrieve_class_and_task _retrieve_class_and_command + end + alias_method :_retrieve_class_and_task, :_retrieve_class_and_command - # Initialize klass using values stored in the @_initializer. - def _parse_initialization_options(args, opts, config) #:nodoc: - stored_args, stored_opts, stored_config = @_initializer + # Initialize klass using values stored in the @_initializer. + def _parse_initialization_options(args, opts, config) #:nodoc: + stored_args, stored_opts, stored_config = @_initializer - args ||= stored_args.dup - opts ||= stored_opts.dup + args ||= stored_args.dup + opts ||= stored_opts.dup - config ||= {} - config = stored_config.merge(_shared_configuration).merge!(config) + config ||= {} + config = stored_config.merge(_shared_configuration).merge!(config) - [ args, opts, config ] - end + [args, opts, config] + end end end