Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add Thor#hide_if #210

Open
wants to merge 1 commit into from

2 participants

Nick Novitski Yehuda Katz
Nick Novitski

hide_if accepts a block, which it stores in the next-defined task, which will evaluate it whenever #hidden? is called, allowing tasks which hide and show themselves dynamically depending on arbitrary conditions. For example, a cleanup script might only appear in the command list if there were any files that needed cleaning up.

Since Tasks can now have varied and dynamic values for #hidden, the class HiddenTask was removed as unnecessary.

Nick Novitski nicknovitski Add Thor#hide_if
hide_if accepts a block, which it stores in the next-defined task, which will evaluate it whenever #hidden? is called, allowing tasks which hide and show themselves dynamically depending on arbitrary conditions.  For example, a cleanup script might only appear in the command list if there were any files that needed cleaning up.

Since Tasks can now have varied and dynamic values for #hidden, the class HiddenTask was removed as unnecessary.
6816ca0
Yehuda Katz
Owner

What is the use-case for this feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 16, 2012
  1. Nick Novitski

    Add Thor#hide_if

    nicknovitski authored
    hide_if accepts a block, which it stores in the next-defined task, which will evaluate it whenever #hidden? is called, allowing tasks which hide and show themselves dynamically depending on arbitrary conditions.  For example, a cleanup script might only appear in the command list if there were any files that needed cleaning up.
    
    Since Tasks can now have varied and dynamic values for #hidden, the class HiddenTask was removed as unnecessary.
This page is out of date. Refresh to see the latest.
12 lib/thor.rb
View
@@ -66,6 +66,12 @@ def long_desc(long_description, options={})
end
end
+ # Sets a block to evaluate if/when the next task will be hidden from the help command.
+ # Overrides the :hide option in desc
+ def hide_if(&block)
+ @hide= block
+ end
+
# Maps an input to a task. If you define:
#
# map "-T" => "list"
@@ -290,8 +296,12 @@ def baseclass #:nodoc:
def create_task(meth) #:nodoc:
if @usage && @desc
- base_class = @hide ? Thor::HiddenTask : Thor::Task
+ base_class = Thor::Task
+
tasks[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options)
+
+ tasks[meth].hidden = @hide
+
@usage, @desc, @long_desc, @method_options, @hide = nil
true
elsif self.all_tasks[meth] || meth == "method_missing"
17 lib/thor/task.rb
View
@@ -11,8 +11,16 @@ def initialize_copy(other) #:nodoc:
self.options = other.options.dup if other.options
end
+ def hidden=(proc_or_bool)
+ @hide = proc_or_bool
+ end
+
def hidden?
- false
+ begin
+ @hide.call
+ rescue NoMethodError
+ @hide
+ end
end
# By default, a task invokes a method in the thor class. You can change this
@@ -108,13 +116,6 @@ def handle_no_method_error?(instance, error, caller)
end
end
- # A task that is hidden in help messages but still invocable.
- class HiddenTask < Task
- def hidden?
- true
- end
- end
-
# A dynamic task that handles method missing scenarios.
class DynamicTask < Task
def initialize(name, options=nil)
6 spec/fixtures/script.thor
View
@@ -35,6 +35,12 @@ class MyScript < Thor
[type]
end
+ desc "blockhidden", "hidden this is"
+ hide_if { $block_hide }
+ def blockhidden
+ true
+ end
+
map "fu" => "zoo"
desc "foo BAR", <<END
13 spec/thor_spec.rb
View
@@ -131,7 +131,7 @@
capture(:stdout) { MyScript.start(["help"]) }.should_not =~ /this is hidden/m
end
- it "but the task is still invokcable not show the task in help" do
+ it "can still invoke the task" do
MyScript.start(["hidden", "yesyes"]).should == ["yesyes"]
end
end
@@ -158,6 +158,17 @@
end
end
+ describe "#hide_if" do
+ it "does not show the task in help if the passed block evaluates to true" do
+ $block_hide = true
+ capture(:stdout) { MyScript.start(["help"]) }.should_not =~ /hidden this is/m
+ end
+ it "does show the task in help if the passed block evaluates to false" do
+ $block_hide = false
+ capture(:stdout) { MyScript.start(["help"]) }.should =~ /hidden this is/m
+ end
+ end
+
describe "#start" do
it "calls a no-param method when no params are passed" do
MyScript.start(["zoo"]).should == true
Something went wrong with that request. Please try again.