Adding support for inline Guard classes rather than requiring a gem #48

Merged
merged 1 commit into from Apr 23, 2011
View
@@ -83,10 +83,15 @@ def add_guard(name, watchers = [], options = {})
end
def get_guard_class(name)
- require "guard/#{name.downcase}"
+ try_to_load_gem name
self.const_get(self.constants.find{|klass_name| klass_name.to_s.downcase == name.downcase })
+ rescue TypeError
+ UI.error "Could not find load find gem 'guard-#{name}' or find class Guard::#{name}"
+ end
+
+ def try_to_load_gem(name)
+ Kernel.require "guard/#{name.downcase}"
rescue LoadError
- UI.error "Could not find gem 'guard-#{name}', please add it in your Gemfile."
end
def locate_guard(name)
View
@@ -30,15 +30,30 @@
end
describe ".get_guard_class" do
- it "should return Guard::RSpec" do
- Guard.get_guard_class('rspec').should == Guard::RSpec
+ it "should report an error if the class is not found" do
+ ::Guard::UI.should_receive(:error)
+ Guard.get_guard_class('notAGuardClass')
end
context 'loaded some nested classes' do
- it "should return Guard::RSpec" do
- require 'guard/rspec'
- Guard::RSpec.class_eval('class NotGuardClass; end')
- Guard.get_guard_class('rspec').should == Guard::RSpec
+ it "should find and return loaded class" do
+ Kernel.should_receive(:require) { |file_name|
+ file_name.should == 'guard/classname'
+ class Guard::Classname
+ end
+ }
+ Guard.get_guard_class('classname').should == Guard::Classname
+ end
+ end
+
+ context 'loaded some inline classes ' do
+ it 'should return inline class' do
+ module Guard
+ class Inline < Guard
+ end
+ end
+
+ Guard.get_guard_class('inline').should == Guard::Inline
end
end
end