Skip to content

Commit

Permalink
Tidying up Thor and Thor::Generator classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jun 2, 2009
1 parent 20032b5 commit 46fb577
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 87 deletions.
100 changes: 46 additions & 54 deletions lib/thor.rb
Expand Up @@ -3,58 +3,6 @@

class Thor

# Invokes a task.
#
# ==== Errors
# Thor::Error:: A Thor error is raised if the user called an undefined task
# or called an exisisting task wrongly.
#
def invoke(meth, *args)
super
rescue ArgumentError => e
backtrace = sans_backtrace(e.backtrace, caller)

if backtrace.empty?
task = self.class[meth]
raise Error, "'#{meth}' was called incorrectly. Call as '#{task.formatted_usage(self.class)}'"
else
raise e
end
rescue NoMethodError => e
if e.message =~ /^undefined method `#{meth}' for #{Regexp.escape(self.inspect)}$/
raise Error, "The #{self.class.namespace} namespace doesn't have a '#{meth}' task"
else
raise e
end
end

# Implement the hooks required by Thor::Base.
#
class << self
protected
def baseclass
Thor
end

def valid_task?(meth)
public_instance_methods.include?(meth) && @usage && @desc
end

def create_task(meth)
tasks[meth.to_s] = Thor::Task.new(meth, @desc, @usage, method_options)
@usage, @desc, @method_options = nil
end

def initialize_added
class_options.merge!(method_options)
@method_options = nil
end
end

include Thor::Base

# Implement specific Thor methods.
#
class << self

# Sets the default task when thor is executed without an explicit task to be called.
Expand Down Expand Up @@ -216,16 +164,34 @@ def help(meth=nil, options={})

protected

def baseclass #:nodoc:
Thor
end

def valid_task?(meth) #:nodoc:
public_instance_methods.include?(meth) && @usage && @desc
end

def create_task(meth) #:nodoc:
tasks[meth.to_s] = Thor::Task.new(meth, @desc, @usage, method_options)
@usage, @desc, @method_options = nil
end

def initialize_added #:nodoc:
class_options.merge!(method_options)
@method_options = nil
end

# Receives a task name (can be nil), and try to get a map from it.
# If a map can't be found use the sent name or the default task.
#
def normalize_task_name(meth)
def normalize_task_name(meth) #:nodoc:
mapping = map[meth.to_s]
meth = mapping || meth || default_task
meth.to_s.gsub('-','_') # treat foo-bar > foo_bar
end

def options_help
def options_help #:nodoc:
unless self.class_options.empty?
list = self.class_options.map do |_, option|
[ option.usage, option.description ]
Expand All @@ -236,9 +202,35 @@ def options_help
puts
end
end
end

# Invokes a task.
#
# ==== Errors
# Thor::Error:: A Thor error is raised if the user called an undefined task
# or called an exisisting task wrongly.
#
def invoke(meth, *args)
super
rescue ArgumentError => e
backtrace = sans_backtrace(e.backtrace, caller)

if backtrace.empty?
task = self.class[meth]
raise Error, "'#{meth}' was called incorrectly. Call as '#{task.formatted_usage(self.class)}'"
else
raise e
end
rescue NoMethodError => e
if e.message =~ /^undefined method `#{meth}' for #{Regexp.escape(self.inspect)}$/
raise Error, "The #{self.class.namespace} namespace doesn't have a '#{meth}' task"
else
raise e
end
end

include Thor::Base

map HELP_MAPPINGS => :help

desc "help [TASK]", "Describe available tasks or one specific task"
Expand Down
61 changes: 28 additions & 33 deletions lib/thor/generator.rb
Expand Up @@ -3,39 +3,6 @@

class Thor::Generator

# Invokes a task.
#
# ==== Errors
# ArgumentError:: raised if the arity of the called task is different from 0.
# NoMethodError:: raised if the method being invoked does not exist.
#
def invoke(meth, *args)
arity = self.method(meth).arity
raise ArgumentError, "Tasks in generators must not accept any argument, but #{meth} has arity #{arity}." if arity != 0
super(meth)
end

# Implement the hooks required by Thor::Base.
#
class << self
protected
def baseclass
Thor::Generator
end

def valid_task?(meth)
public_instance_methods.include?(meth)
end

def create_task(meth)
tasks[meth.to_s] = Thor::Task.new(meth, nil, nil, nil)
end
end

include Thor::Base

# Implement specific Thor::Generator logic.
#
class << self

# Start in generators works differently. It invokes all tasks inside the class.
Expand Down Expand Up @@ -81,5 +48,33 @@ def help(options={})
end
end

protected

def baseclass #:nodoc:
Thor::Generator
end

def valid_task?(meth) #:nodoc:
public_instance_methods.include?(meth)
end

def create_task(meth) #:nodoc:
tasks[meth.to_s] = Thor::Task.new(meth, nil, nil, nil)
end

end

# Invokes a task.
#
# ==== Errors
# ArgumentError:: raised if the arity of the called task is different from 0.
# NoMethodError:: raised if the method being invoked does not exist.
#
def invoke(meth, *args)
arity = self.method(meth).arity
raise ArgumentError, "Tasks in generators must not accept any argument, but #{meth} has arity #{arity}." if arity != 0
super(meth)
end

include Thor::Base
end

0 comments on commit 46fb577

Please sign in to comment.