Class: Thor::Task

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

Direct Known Subclasses

PackageTask

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Task

Returns a new instance of Task.



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

def initialize(*args)
  # keep the original opts - we need them later on
  @options = args[3] || {}
  super
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



5
6
7
# File 'lib/thor/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/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/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/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/lib/thor/task.rb', line 5

def usage
  @usage
end

Class Method Details

.dynamic(meth, klass) ⇒ Object



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

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



69
70
71
72
# File 'lib/thor/lib/thor/task.rb', line 69

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

#full_optsObject



65
66
67
# File 'lib/thor/lib/thor/task.rb', line 65

def full_opts
  @_full_opts ||= Options.new((klass.opts || {}).merge(@options))
end

#namespace(remove_default = true) ⇒ Object



50
51
52
# File 'lib/thor/lib/thor/task.rb', line 50

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

#parse(obj, args) ⇒ Object



17
18
19
20
21
# File 'lib/thor/lib/thor/task.rb', line 17

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

#run(obj, *params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/thor/lib/thor/task.rb', line 23

def run(obj, *params)
  raise NoMethodError, "the `#{meth}' task of #{obj.class} is private" if
    (obj.private_methods + obj.protected_methods).include?(meth)
  
  obj.invoke(meth, *params)
rescue ArgumentError => e
  
  # backtrace sans anything in this file
  backtrace = e.backtrace.reject {|frame| frame =~ /^#{Regexp.escape(__FILE__)}/}
  # also nix anything in thor.rb
  backtrace = backtrace.reject { |frame| frame =~ /\/thor.rb/ }
  
  # 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



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

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