Permalink
Browse files

Scientist nicely handles commands that cant be redefined

  • Loading branch information...
1 parent 7ca283e commit 7726d323a59af93efefd78c913744d20b835f610 @cldwalker committed May 25, 2010
Showing with 13 additions and 1 deletion.
  1. +7 −1 lib/boson/scientist.rb
  2. +6 −0 test/scientist_test.rb
View
@@ -46,6 +46,8 @@ def redefine_command(obj, command)
[command.name, command.alias].compact.each {|e|
obj.instance_eval("class<<self;self;end").send(:define_method, e, cmd_block)
}
+ rescue Error
+ $stderr.puts "Error: #{$!.message}"
end
# A wrapper around redefine_command that doesn't depend on a Command object. Rather you
@@ -73,7 +75,11 @@ def commandify(obj, hash)
# The actual method which redefines a command's original method
def redefine_command_block(obj, command)
- object_methods(obj)[command.name] ||= obj.method(command.name)
+ object_methods(obj)[command.name] ||= begin
+ obj.method(command.name)
+ rescue NameError
+ raise Error, "No method exists to redefine command '#{command.name}'."
+ end
lambda {|*args|
Scientist.translate_and_render(obj, command, args) {|args|
Scientist.object_methods(obj)[command.name].call(*args)
View
@@ -273,6 +273,12 @@ def render_expected(options=nil)
command({:args=>2, :options=>nil, :render_options=>{:fields=>:array}}, ["--fields f1 ab ok"])
end
+ it "redefine_command prints error for command with nonexistant method" do
+ capture_stderr {
+ Scientist.redefine_command Object.new, Command.new(:name=>'blah', :lib=>'blah')
+ }.should =~ /Error: No method.*'blah'/
+ end
+
describe "global options:" do
def local_and_global(*args)
Scientist.stubs(:can_render?).returns(false) # turn off rendering caused by :render_options

0 comments on commit 7726d32

Please sign in to comment.