Class: Thor::Task

Inherits:
Struct
  • Object
show all
Defined in:
lib/thor/task.rb

Direct Known Subclasses

InstallTask, PackageTask, SpecTask

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options=nil)
  super(name.to_s, description, usage, options || {})
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



2
3
4
# File 'lib/thor/task.rb', line 2

def description
  @description
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/thor/task.rb', line 2

def name
  @name
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



2
3
4
# File 'lib/thor/task.rb', line 2

def options
  @options
end

#usageObject

Returns the value of attribute usage

Returns:

  • (Object)

    the current value of 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_optionsObject

Returns the options usage for this task.



62
63
64
# File 'lib/thor/task.rb', line 62

def formatted_options
  @formatted_options ||= 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_options}"
  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.options = other.options.dup if other.options
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_descriptionObject



20
21
22
# File 'lib/thor/task.rb', line 20

def short_description
  description.split("\n").first if description
end