Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add Thor#disable #211

Closed
wants to merge 1 commit into from

2 participants

@nicknovitski

This method takes a string and a block. While the block evaluates to true, the following task is hidden from help, and invoking it will raise an exception with the passed string as a message.

@nicknovitski nicknovitski Add Thor#disable
This method takes a string and a block.  While the block evaluates to true, the following task is hidden from help, and invoking it will raise an exception with the passed string as a message.
cd99cb6
@wycats
Owner

You should use no_tasks {} to surround methods that are not tasks.

@wycats wycats closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 20, 2012
  1. @nicknovitski

    Add Thor#disable

    nicknovitski authored
    This method takes a string and a block.  While the block evaluates to true, the following task is hidden from help, and invoking it will raise an exception with the passed string as a message.
This page is out of date. Refresh to see the latest.
View
20 lib/thor.rb
@@ -66,6 +66,20 @@ def long_desc(long_description, options={})
end
end
+ # Disables the next task. It will be hidden, and if invoked, it will raise
+ # an exception with the passed message.
+ #
+ # If run with a block, sets a dynamic condition under which the next task
+ # will be disabled.
+ #
+ # === Parameters
+ # exception message<String>
+ #
+ def disable(err_message, &block)
+ @disable_proc = block
+ @disable_message = err_message
+ end
+
# Maps an input to a task. If you define:
#
# map "-T" => "list"
@@ -292,7 +306,11 @@ def create_task(meth) #:nodoc:
if @usage && @desc
base_class = @hide ? Thor::HiddenTask : Thor::Task
tasks[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options)
- @usage, @desc, @long_desc, @method_options, @hide = nil
+ tasks[meth].disable(@disable_message, @disable_proc)
+ @usage, @desc, @long_desc, @method_options, @hide, @disable_proc = nil
+
+
+
true
elsif self.all_tasks[meth] || meth == "method_missing"
true
View
21 lib/thor/task.rb
@@ -12,12 +12,31 @@ def initialize_copy(other) #:nodoc:
end
def hidden?
- false
+ if @disable.nil?
+ false
+ else
+ disabled?
+ end
+ end
+
+ def disable(message="disabled task", proc)
+ @disable, @disable_message = proc, message
+ end
+
+ def disabled?
+ begin
+ @disable.call
+ rescue NoMethodError
+ false
+ end
end
# By default, a task invokes a method in the thor class. You can change this
# implementation to create custom tasks.
def run(instance, args=[])
+
+ raise Exception.new(@disable_message) if disabled?
+
arity = nil
if private_method?(instance)
View
8 spec/fixtures/script.thor
@@ -35,6 +35,14 @@ class MyScript < Thor
[type]
end
+ desc "disabled", "This task is currenly enabled"
+ disable "This task is currently disabled" do
+ $block_disable
+ end
+ def disabled
+ "ran"
+ end
+
map "fu" => "zoo"
desc "foo BAR", <<END
View
25 spec/thor_spec.rb
@@ -137,6 +137,31 @@
end
end
+ describe "#disable" do
+ context "when the passed block evaluates to true" do
+ before do
+ $block_disable = true
+ end
+ it "does not show the task in help" do
+ capture(:stdout) { MyScript.start(["help"]) }.should_not =~ /This task is currenly enabled/m
+ end
+ it "raises an exception instead of running the task" do
+ expect { MyScript.start(["disabled"]) }.to raise_error(Exception, /This task is currently disabled/)
+ end
+ end
+ context "when the passed block evaluates to false" do
+ before do
+ $block_disable = false
+ end
+ it "does show the task in help" do
+ capture(:stdout) { MyScript.start(["help"]) }.should =~ /This task is currenly enabled/m
+ end
+ it "can run the task as normal" do
+ MyScript.start(["disabled"]).should == "ran"
+ end
+ end
+ end
+
describe "#method_options" do
it "sets default options if called before an initializer" do
options = MyChildScript.class_options
Something went wrong with that request. Please try again.