Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Drying up maxima and bringing dynamic tasks back to the game.

  • Loading branch information...
commit 95d1fcb3b8221c54e43fcdde1f7a76dc5bc20eb9 1 parent a50b5b1
@josevalim josevalim authored
Showing with 17 additions and 10 deletions.
  1. +1 −1  lib/thor.rb
  2. +14 −7 lib/thor/base.rb
  3. +2 −2 spec/base_spec.rb
View
2  lib/thor.rb
@@ -23,7 +23,7 @@ def help(task = nil)
puts "Options"
puts "-------"
self.class.tasks.each do |_, task|
- format = "%-" + (self.class.maxima.usage + self.class.maxima.opt + 4).to_s + "s"
+ format = "%-" + (self.class.maxima.usage + self.class.maxima.options + 4).to_s + "s"
print format % ("#{task.formatted_usage(self, false)}")
puts task.description.split("\n").first
end
View
21 lib/thor/base.rb
@@ -5,6 +5,9 @@
class Thor
+ class Maxima < Struct.new(:description, :usage, :options)
+ end
+
# Holds class method for Thor class. If you want to create Thor tasks, this
# is where you should look at.
#
@@ -114,7 +117,7 @@ def method_options(options)
# Hash[path<String> => Class]
#
def subclass_files
- @subclass_files ||= Hash.new {|h,k| h[k] = []}
+ @subclass_files ||= Hash.new{ |h,k| h[k] = [] }
end
# Returns the subclasses. Subclasses are dynamically added to the array when
@@ -136,7 +139,8 @@ def tasks
@tasks ||= from_superclass(:tasks, Thor::CoreExt::OrderedHash.new)
end
- # A shortcut to retrieve a specific task from this Thor class.
+ # Retrieve a specific task from this Thor class. If the desired Task cannot
+ # be found, returns a dynamic Thor::Task that will map to the given name.
#
# ==== Parameters
# name<Symbol>:: the name of the task to be retrieved
@@ -145,7 +149,7 @@ def tasks
# Thor::Task
#
def [](name)
- tasks[name.to_s]
+ tasks[name.to_s] || Thor::Task.dynamic(name)
end
# Returns and sets the default options for this class. It can be done in
@@ -178,10 +182,13 @@ def default_options(options=nil)
def maxima
@maxima ||= begin
- max_usage = tasks.map {|_, t| t.usage}.max {|x,y| x.to_s.size <=> y.to_s.size}.size
- max_desc = tasks.map {|_, t| t.description}.max {|x,y| x.to_s.size <=> y.to_s.size}.size
- max_opts = tasks.map {|_, t| t.full_options(self).formatted_usage}.max {|x,y| x.to_s.size <=> y.to_s.size}.size
- Struct.new(:description, :usage, :opt).new(max_desc, max_usage, max_opts)
+ compare = lambda { |x,y| x.to_s.size <=> y.to_s.size }
+
+ max_usage = tasks.map{ |_, t| t.usage }.max(&compare).size
+ max_desc = tasks.map{ |_, t| t.description }.max(&compare).size
+ max_opts = tasks.map{ |_, t| t.full_options(self).formatted_usage }.max(&compare).size
+
+ Thor::Maxima.new(max_desc, max_usage, max_opts)
end
end
View
4 spec/base_spec.rb
@@ -225,7 +225,7 @@ def hello
Scripts::MyGrandChildScript[:foo].class.must == Thor::Task
end
- xit "returns a dynamic task to allow method missing invocation" do
+ it "returns a dynamic task to allow method missing invocation" do
MyScript[:none].class.must == Thor::Task
MyScript[:none].description =~ /dynamic/
end
@@ -291,7 +291,7 @@ def hello
it "returns the maximum length for usage, description and options" do
MyScript.maxima.description.must == 64
MyScript.maxima.usage.must == 28
- MyScript.maxima.opt.must == 19
+ MyScript.maxima.options.must == 19
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.