Class: Thor::Task
- Inherits:
-
Struct
- Object
- Struct
- Thor::Task
- Defined in:
- lib/thor/task.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#usage ⇒ Object
Returns the value of attribute usage.
Class Method Summary collapse
-
.dynamic(name) ⇒ Object
Creates a dynamic task.
Instance Method Summary collapse
-
#formatted_arguments(klass) ⇒ Object
Injects the class arguments into the task usage.
-
#formatted_options ⇒ Object
Returns the options usage for this task.
-
#formatted_usage(klass = nil, namespace = false) ⇒ Object
Returns the formatted usage.
-
#initialize(name, description, usage, options = nil) ⇒ Task
constructor
A new instance of Task.
- #initialize_copy(other) ⇒ Object
-
#run(instance, args = []) ⇒ Object
By default, a task invokes a method in the thor class.
- #short_description ⇒ Object
Constructor Details
#initialize(name, description, usage, options = nil) ⇒ Task
Returns a new instance of Task.
11 12 13 |
# File 'lib/thor/task.rb', line 11 def initialize(name, description, usage, =nil) super(name.to_s, description, usage, || {}) end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description
2 3 4 |
# File 'lib/thor/task.rb', line 2 def description @description end |
#name ⇒ Object
Returns the value of attribute name
2 3 4 |
# File 'lib/thor/task.rb', line 2 def name @name end |
#options ⇒ Object
Returns the value of attribute options
2 3 4 |
# File 'lib/thor/task.rb', line 2 def @options end |
#usage ⇒ Object
Returns the value of attribute usage
2 3 4 |
# File 'lib/thor/task.rb', line 2 def usage @usage end |
Class Method Details
.dynamic(name) ⇒ Object
Creates a dynamic task. Dynamic tasks are created on demand to allow method missing calls (since a method missing does not have a task object for it).
7 8 9 |
# File 'lib/thor/task.rb', line 7 def self.dynamic(name) new(name, "A dynamically-generated task", name.to_s) end |
Instance Method Details
#formatted_arguments(klass) ⇒ Object
Injects the class arguments into the task usage.
50 51 52 53 54 55 56 57 58 |
# File 'lib/thor/task.rb', line 50 def formatted_arguments(klass) if klass && !klass.arguments.empty? usage.to_s.gsub(/^#{name}/) do |match| match << " " << klass.arguments.map{ |a| a.usage }.join(' ') end else usage.to_s end end |
#formatted_options ⇒ Object
Returns the options usage for this task.
62 63 64 |
# File 'lib/thor/task.rb', line 62 def @formatted_options ||= .map{ |_, o| o.usage }.sort.join(" ") end |
#formatted_usage(klass = nil, namespace = false) ⇒ Object
Returns the formatted usage. If a class is given, the class arguments are injected in the usage.
39 40 41 42 43 44 45 46 |
# File 'lib/thor/task.rb', line 39 def formatted_usage(klass=nil, namespace=false) formatted = '' formatted << "#{klass.namespace.gsub(/^default/,'')}:" if klass && namespace formatted << formatted_arguments(klass) formatted << " #{}" formatted.strip! formatted end |
#initialize_copy(other) ⇒ Object
15 16 17 18 |
# File 'lib/thor/task.rb', line 15 def initialize_copy(other) super(other) self. = other..dup if other. end |
#run(instance, args = []) ⇒ Object
By default, a task invokes a method in the thor class. You can change this implementation to create custom tasks.
27 28 29 30 31 32 33 34 |
# File 'lib/thor/task.rb', line 27 def run(instance, args=[]) raise UndefinedTaskError, "the '#{name}' task of #{instance.class} is private" unless public_method?(instance) instance.send(name, *args) rescue ArgumentError => e parse_argument_error(instance, e, caller) rescue NoMethodError => e parse_no_method_error(instance, e) end |
#short_description ⇒ Object
20 21 22 |
# File 'lib/thor/task.rb', line 20 def short_description description.split("\n").first if description end |