From a0b6ecacdb9123b54eb60a21d136744628eb4702 Mon Sep 17 00:00:00 2001 From: Jeff Sacks Date: Thu, 21 Apr 2011 16:39:46 -0500 Subject: [PATCH] adding support for inline classes --- lib/guard.rb | 9 +++++++-- spec/guard_spec.rb | 27 +++++++++++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/guard.rb b/lib/guard.rb index f268b99..df2109b 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -83,10 +83,15 @@ module Guard 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) diff --git a/spec/guard_spec.rb b/spec/guard_spec.rb index 97f6d6a..0162c06 100644 --- a/spec/guard_spec.rb +++ b/spec/guard_spec.rb @@ -30,15 +30,30 @@ describe Guard do 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