Skip to content

Commit

Permalink
refactor get_guard_class to first try the constant and fallback to …
Browse files Browse the repository at this point in the history
…require

This enables defining inline guard handlers to override ones present in gems,
e.g. you could define an inline Guard::RSpec handler and have guard use that
instead of first trying to load 'guard/rspec' from the 'guard-rspec' gem.

Also gets rid of the ill-named method `try_to_load_gem`. Handlers are simply
found in the $LOAD_PATH and don't have to necessarily come from gems.
  • Loading branch information
mislav committed Jun 7, 2011
1 parent 9d63f1a commit 17a654b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
24 changes: 15 additions & 9 deletions lib/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,21 @@ def add_guard(name, watchers = [], options = {})
end

def get_guard_class(name)
try_to_load_gem(name)
self.const_get(self.constants.find { |klass_name| klass_name.to_s.downcase == name.to_s.downcase.gsub('-', '') })
rescue TypeError
UI.error "Could not find load find gem 'guard-#{name}' or find class Guard::#{name}"
end

def try_to_load_gem(name)
require "guard/#{name.to_s.downcase}"
rescue LoadError
try_require = false
const_name = name.to_s.downcase.gsub('-', '')
begin
require "guard/#{name.downcase}" if try_require
self.const_get(self.constants.find {|c| c.to_s.downcase == const_name })
rescue TypeError
unless try_require
try_require = true
retry
else
UI.error "Could not find class Guard::#{const_name.capitalize}"
end
rescue LoadError
UI.error "Could not load 'guard/#{name.downcase}' or find class Guard::#{const_name.capitalize}"
end
end

def locate_guard(name)
Expand Down
43 changes: 19 additions & 24 deletions spec/guard_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'guard/guard'

describe Guard do

Expand Down Expand Up @@ -41,35 +42,41 @@
end

describe ".get_guard_class" do
after do
[:Classname, :DashedClassName, :Inline].each do |const|
Guard.send(:remove_const, const) rescue nil
end
end

it "reports an error if the class is not found" do
::Guard::UI.should_receive(:error)
Guard.get_guard_class('notAGuardClass')
end

context 'with a nested Guard class' do
it "returns the Guard class when passed a Symbol" do
Guard.should_receive(:try_to_load_gem) { |classname|
classname.should == :classname
it "resolves the Guard class from string" do
Guard.should_receive(:require) { |classname|
classname.should == 'guard/classname'
class Guard::Classname
end
}
Guard.get_guard_class(:classname).should == Guard::Classname
Guard.get_guard_class('classname').should == Guard::Classname
end
it "returns the Guard class when passed a String" do
Guard.should_receive(:try_to_load_gem) { |classname|
classname.should == 'classname'

it "resolves the Guard class from symbol" do
Guard.should_receive(:require) { |classname|
classname.should == 'guard/classname'
class Guard::Classname
end
}
Guard.get_guard_class('classname').should == Guard::Classname
Guard.get_guard_class(:classname).should == Guard::Classname
end
end

context 'with a name with dashes' do
it "returns the Guard class" do
Guard.should_receive(:try_to_load_gem) { |classname|
classname.should == 'dashed-class-name'
Guard.should_receive(:require) { |classname|
classname.should == 'guard/dashed-class-name'
class Guard::DashedClassName
end
}
Expand All @@ -84,23 +91,11 @@ class Inline < Guard
end
end

Guard.should_not_receive(:require)
Guard.get_guard_class('inline').should == Guard::Inline
end
end
end

describe ".try_to_load_gem" do
class Guard::Classname
end

it "reports an error if the class is not found" do
Guard.get_guard_class('classname').should be_true
end

it "reports an error if the class is not found" do
Guard.get_guard_class(:classname).should be_true
end
end

describe ".locate_guard" do
it "returns the path of a Guard gem" do
Expand Down

0 comments on commit 17a654b

Please sign in to comment.