Class: Thor::Task

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

Direct Known Subclasses

PackageTask

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



5
6
7
# File 'lib/thor/task.rb', line 5

def description
  @description
end

#klassObject

Returns the value of attribute klass

Returns:

  • (Object)

    the current value of klass



5
6
7
# File 'lib/thor/task.rb', line 5

def klass
  @klass
end

#methObject

Returns the value of attribute meth

Returns:

  • (Object)

    the current value of meth



5
6
7
# File 'lib/thor/task.rb', line 5

def meth
  @meth
end

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



5
6
7
# File 'lib/thor/task.rb', line 5

def opts
  @opts
end

#usageObject

Returns the value of attribute usage

Returns:

  • (Object)

    the current value of usage



5
6
7
# File 'lib/thor/task.rb', line 5

def usage
  @usage
end

Class Method Details

.dynamic(meth, klass) ⇒ Object



6
7
8
# File 'lib/thor/task.rb', line 6

def self.dynamic(meth, klass)
  new(meth, "A dynamically-generated task", meth.to_s, nil, klass)
end

Instance Method Details

#formatted_usage(namespace = false) ⇒ Object



54
55
56
57
# File 'lib/thor/task.rb', line 54

def formatted_usage(namespace = false)
  (namespace ? self.namespace + ':' : '') + usage +
    (opts ? " " + opts.formatted_usage : "")
end

#namespace(remove_default = true) ⇒ Object



39
40
41
# File 'lib/thor/task.rb', line 39

def namespace(remove_default = true)
  Thor::Util.constant_to_thor_path(klass, remove_default)
end

#parse(obj, args) ⇒ Object



10
11
12
13
14
# File 'lib/thor/task.rb', line 10

def parse(obj, args)
  list, hash = parse_args(args)
  obj.options = hash
  run(obj, *list)
end

#run(obj, *params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/thor/task.rb', line 16

def run(obj, *params)
  raise NoMethodError, "the `#{meth}' task of #{obj.class} is private" if
    (obj.private_methods + obj.protected_methods).include?(meth)
  
  obj.send(meth, *params)
rescue ArgumentError => e
  # backtrace sans anything in this file
  backtrace = e.backtrace.reject {|frame| frame =~ /^#{Regexp.escape(__FILE__)}/}
  # and sans anything that got us here
  backtrace -= caller
  raise e unless backtrace.empty?

  # okay, they really did call it wrong
  raise Error, "`#{meth}' was called incorrectly. Call as `#{formatted_usage}'"
rescue NoMethodError => e
  begin
    raise e unless e.message =~ /^undefined method `#{meth}' for #{Regexp.escape(obj.inspect)}$/
  rescue
    raise e
  end
  raise Error, "The #{namespace false} namespace doesn't have a `#{meth}' task"
end

#with_klass(klass) ⇒ Object



43
44
45
46
47
# File 'lib/thor/task.rb', line 43

def with_klass(klass)
  new = self.dup
  new.klass = klass
  new
end