Skip to content

Instantly share code, notes, and snippets.

@dstrelau
Created October 10, 2008 19:31
Show Gist options
  • Save dstrelau/16137 to your computer and use it in GitHub Desktop.
Save dstrelau/16137 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# module: abc
require 'rubygems'
require 'thor'
class Abc < Thor
desc 'debug', ''
def debug
puts "debug"
end
class Xyz < Thor
desc "debug", "namespaced task"
def debug
puts "~ XYZ namespace ~"
puts "debug"
end
end
def method_missing(meth, *args)
task = Thor["abc:#{meth.to_s}"]
task.parse task.klass.new, ARGV[1..-1]
end
end
Abc.start
[abc] thor -T
The abc namespace doesn't have a `-T' task
Tasks
-----
abc:debug
abc:foo:bar do the bar
[abc] thor abc:debug
The abc namespace doesn't have a `abc:debug' task
tasks:
debug
help
[abc] thor abc:foo:bar
The abc namespace doesn't have a `abc:foo:bar' task
BAR!
tasks:
bar
help
[abc] ./abc.thor debug
tasks:
debug
help
[abc] ./abc.thor foo:bar
The abc namespace doesn't have a `foo:bar' task
#!/usr/bin/env ruby
# module: abc
require 'rubygems'
require 'thor'
class Abc < Thor
desc 'debug', ''
def debug
puts "tasks:"
puts self.class.tasks.map{|k,v| k}
end
class Foo < Thor
desc "bar", "do the bar"
def bar
puts "BAR!"
puts "tasks:"
puts self.class.tasks.map{|k,v| k}
end
end
end
Abc.start
diff --git a/spec/thor_spec.rb b/spec/thor_spec.rb
index 0a4a8ef..6772bf7 100644
--- a/spec/thor_spec.rb
+++ b/spec/thor_spec.rb
@@ -5,6 +5,13 @@ class MyApp < Thor
map "-T" => :animal, ["-f", "--foo"] => :foo
+ class More < Thor
+ desc "stuff", "a namespaced task"
+ def stuff
+ true
+ end
+ end
+
desc "zoo", "zoo around"
def zoo
true
@@ -135,6 +142,10 @@ describe "thor" do
lambda { MyApp.start(["what"]) }.must raise_error(NoMethodError, "the `what' task of MyApp is private")
end
+ it "calls a namespaced method" do
+ MyApp.start(["more:stuff"]).must == true
+ end
+
it "provides useful help info for a simple method" do
capture(:stdout) { MyApp.start(["help"]) }.must =~ /zoo +zoo around/
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment