Thor has a special class called Thor::Group. The main difference to Thor class is that it invokes all tasks at once. It also include some methods that allows invocations to be done at the class method, which are not available to Thor tasks.
The description for this Thor::Group. If none is provided, but a source root exists, tries to find the USAGE one folder above it, otherwise searches in the superclass.
description | The description for this Thor::Group. |
# File lib/thor/group.rb, line 16 16: def desc(description=nil) 17: case description 18: when nil 19: @desc ||= from_superclass(:desc, nil) 20: else 21: @desc = description 22: end 23: end
Prints help information.
short | When true, shows only usage. |
# File lib/thor/group.rb, line 30 30: def help(shell) 31: shell.say "Usage:" 32: shell.say " #{banner}\n" 33: shell.say 34: class_options_help(shell) 35: shell.say self.desc if self.desc 36: end
Invoke the given namespace or class given. It adds an instance method that will invoke the klass and task. You can give a block to configure how it will be invoked.
The namespace/class given will have its options showed on the help usage. Check invoke_from_option for more information.
# File lib/thor/group.rb, line 57 57: def invoke(*names, &block) 58: options = names.last.is_a?(Hash) ? names.pop : {} 59: verbose = options.fetch(:verbose, true) 60: 61: names.each do |name| 62: invocations[name] = false 63: invocation_blocks[name] = block if block_given? 64: 65: class_eval def _invoke_#{name.to_s.gsub(/\W/, '_')} klass, task = self.class.prepare_for_invocation(nil, #{name.inspect}) if klass say_status :invoke, #{name.inspect}, #{verbose.inspect} block = self.class.invocation_blocks[#{name.inspect}] _invoke_for_class_method klass, task, &block else say_status :error, %(#{name.inspect} [not found]), :red end end, __FILE__, __LINE__ 66: end 67: end
Invoke a thor class based on the value supplied by the user to the given option named “name”. A class option must be created before this method is invoked for each name given.
class GemGenerator < Thor::Group class_option :test_framework, :type => :string invoke_from_option :test_framework end
In some cases, you want to invoke a thor class if some option is true or false. This is automatically handled by invoke_from_option. Then the option name is used to invoke the generator.
In some cases you want to customize how a specified hook is going to be invoked. You can do that by overwriting the class method prepare_for_invocation. The class method must necessarily return a klass and an optional task.
You can also supply a block to customize how the option is giong to be invoked. The block receives two parameters, an instance of the current class and the klass to be invoked.
# File lib/thor/group.rb, line 111 111: def invoke_from_option(*names, &block) 112: options = names.last.is_a?(Hash) ? names.pop : {} 113: verbose = options.fetch(:verbose, :white) 114: 115: names.each do |name| 116: unless class_options.key?(name) 117: raise ArgumentError, "You have to define the option #{name.inspect} " << 118: "before setting invoke_from_option." 119: end 120: 121: invocations[name] = true 122: invocation_blocks[name] = block if block_given? 123: 124: class_eval def _invoke_from_option_#{name.to_s.gsub(/\W/, '_')} return unless options[#{name.inspect}] value = options[#{name.inspect}] value = #{name.inspect} if TrueClass === value klass, task = self.class.prepare_for_invocation(#{name.inspect}, value) if klass say_status :invoke, value, #{verbose.inspect} block = self.class.invocation_blocks[#{name.inspect}] _invoke_for_class_method klass, task, &block else say_status :error, %(\#{value} [not found]), :red end end, __FILE__, __LINE__ 125: end 126: end
Returns tasks ready to be printed.
# File lib/thor/group.rb, line 200 200: def printable_tasks(*) 201: item = [] 202: item << banner 203: item << (desc ? "# #{desc.gsub(/\s+/m,' ')}" : "") 204: [item] 205: end
Remove a previously added invocation.
remove_invocation :test_framework
# File lib/thor/group.rb, line 150 150: def remove_invocation(*names) 151: names.each do |name| 152: remove_task(name) 153: remove_class_option(name) 154: invocations.delete(name) 155: invocation_blocks.delete(name) 156: end 157: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.