Class: Thor::Task
- Inherits:
-
Struct
- Object
- Struct
- Thor::Task
- Defined in:
- lib/thor/lib/thor/task.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#meth ⇒ Object
Returns the value of attribute meth.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#usage ⇒ Object
Returns the value of attribute usage.
Class Method Summary collapse
Instance Method Summary collapse
- #formatted_usage(namespace = false) ⇒ Object
- #full_opts ⇒ Object
-
#initialize(*args) ⇒ Task
constructor
A new instance of Task.
- #namespace(remove_default = true) ⇒ Object
- #parse(obj, args) ⇒ Object
- #run(obj, *params) ⇒ Object
- #with_klass(klass) ⇒ Object
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
#description ⇒ Object
Returns the value of attribute description
5 6 7 |
# File 'lib/thor/lib/thor/task.rb', line 5 def description @description end |
#klass ⇒ Object
Returns the value of attribute klass
5 6 7 |
# File 'lib/thor/lib/thor/task.rb', line 5 def klass @klass end |
#meth ⇒ Object
Returns the value of attribute meth
5 6 7 |
# File 'lib/thor/lib/thor/task.rb', line 5 def meth @meth end |
#opts ⇒ Object
Returns the value of attribute opts
5 6 7 |
# File 'lib/thor/lib/thor/task.rb', line 5 def opts @opts end |
#usage ⇒ Object
Returns the value of attribute 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_opts ⇒ Object
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. = 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. =~ /^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 |