guard/guard — Issue #175: Guard.get_guard_class should convert snake_case to CamelCase to find class names
Author: benolee
Created: 2011-10-28 19:33:09+00:00
State: open

# The Problem

Currently, the singleton method [Guard.get_guard_class](https://github.com/guard/guard/blob/v0.8.8/lib/guard.rb#L398) removes dashes and compares all constants under the Guard module to the given guard name (say, in a Guardfile) after downcasing both. Finding Guard subclasses this way seems overly restrictive.

For example, given a guard with gem name `guard-foo_bar_baz`, and the following Guardfile:

```ruby
# Guardfile
guard :foo_bar_baz do
end
```

and the following class definition:

```ruby
# lib/guard/foo_bar_baz.rb
module Guard
class FooBarBaz < Guard
#...
end
end
```

then Guard looks for a class that downcases to `foo_bar_baz`, which doesn't exist. I could rename my class to `Guard::Foo_Bar_Baz`, but that goes against ruby class naming conventions.

If we try to change the Guardfile to take advantage of the `gsub('-','')`, for instance:

```ruby
# Guardfile
guard 'foo-bar-baz' do
end
```

then it correctly finds the class name, but it looks for the class definition in `lib/guard/foo-bar-baz.rb`, which goes against ruby file naming conventions.

# Solutions?

It seems to me that the intent of the `gsub` in [Guard.get_guard_class](https://github.com/guard/guard/blob/v0.8.8/lib/guard.rb#L401) was to remove **underscores**, not **dashes**. If this is the case, that would take care of the problem above, but it still seems like a bad lookup.

A better solution might be to borrow the `camelize` method from ActiveSupport, for example:

https://gist.github.com/1323225

however, this might break other gems such as guard-rspec, whose class name is [`RSpec`](https://github.com/guard/guard-rspec/blob/master/lib/guard/rspec.rb#L5), not `Rspec`.

It seems to me that there should be a better way for Guard to keep track of what guards exist, and accessing the class names shouldn't just be a lookup of all constants under the Guard module.

# TL;DR

My snake_cased guard name fails to match my CamelCased class name.