Allow more complex conditions when searching for guards

This commit is contained in:
Rémy Coutable 2011-09-23 00:39:27 +02:00
parent 0a60575dde
commit 41ada16595
2 changed files with 35 additions and 12 deletions

View File

@ -56,8 +56,12 @@ module Guard
@guards.find_all { |guard| guard.class.to_s.downcase.sub('guard::', '') =~ filter } @guards.find_all { |guard| guard.class.to_s.downcase.sub('guard::', '') =~ filter }
when Hash when Hash
filter.inject(@guards) do |matches, (k, v)| filter.inject(@guards) do |matches, (k, v)|
if k.to_sym == :name
matches.find_all { |guard| guard.class.to_s.downcase.sub('guard::', '') == v.to_s.downcase.gsub('-', '') }
else
matches.find_all { |guard| guard.send(k).to_sym == v.to_sym } matches.find_all { |guard| guard.send(k).to_sym == v.to_sym }
end end
end
else else
@guards @guards
end end

View File

@ -63,10 +63,14 @@ describe Guard do
subject do subject do
guard = ::Guard.setup guard = ::Guard.setup
@guard_foo_bar = Guard::FooBar.new([], { :group => 'backend' }) @guard_foo_bar_backend = Guard::FooBar.new([], { :group => 'backend' })
@guard_foo_baz = Guard::FooBaz.new([], { :group => 'frontend' }) @guard_foo_bar_frontend = Guard::FooBar.new([], { :group => 'frontend' })
guard.instance_variable_get("@guards").push(@guard_foo_bar) @guard_foo_baz_backend = Guard::FooBaz.new([], { :group => 'backend' })
guard.instance_variable_get("@guards").push(@guard_foo_baz) @guard_foo_baz_frontend = Guard::FooBaz.new([], { :group => 'frontend' })
guard.instance_variable_get("@guards").push(@guard_foo_bar_backend)
guard.instance_variable_get("@guards").push(@guard_foo_bar_frontend)
guard.instance_variable_get("@guards").push(@guard_foo_baz_backend)
guard.instance_variable_get("@guards").push(@guard_foo_baz_frontend)
guard guard
end end
@ -76,11 +80,11 @@ describe Guard do
describe "find a guard by as string/symbol" do describe "find a guard by as string/symbol" do
it "find a guard by a string" do it "find a guard by a string" do
subject.guards('foo-bar').should eql @guard_foo_bar subject.guards('foo-bar').should eql @guard_foo_bar_backend
end end
it "find a guard by a symbol" do it "find a guard by a symbol" do
subject.guards(:'foo-bar').should eql @guard_foo_bar subject.guards(:'foo-bar').should eql @guard_foo_bar_backend
end end
it "returns nil if guard is not found" do it "returns nil if guard is not found" do
@ -90,7 +94,7 @@ describe Guard do
describe "find guards matching a regexp" do describe "find guards matching a regexp" do
it "with matches" do it "with matches" do
subject.guards(/^foo/).should eql [@guard_foo_bar, @guard_foo_baz] subject.guards(/^foobar/).should eql [@guard_foo_bar_backend, @guard_foo_bar_frontend]
end end
it "without matches" do it "without matches" do
@ -100,16 +104,31 @@ describe Guard do
describe "find guards by their group" do describe "find guards by their group" do
it "group name is a string" do it "group name is a string" do
subject.guards(:group => 'backend').should eql [@guard_foo_bar] subject.guards(:group => 'backend').should eql [@guard_foo_bar_backend, @guard_foo_baz_backend]
end end
it "group name is a symbol" do it "group name is a symbol" do
subject.guards(:group => :frontend).should eql [@guard_foo_baz] subject.guards(:group => :frontend).should eql [@guard_foo_bar_frontend, @guard_foo_baz_frontend]
end end
it "returns [] if guard is not found" do it "returns [] if guard is not found" do
subject.guards(:group => :unknown).should eql [] subject.guards(:group => :unknown).should eql []
end end
end end
describe "find guards by their group & name" do
it "group name is a string" do
subject.guards(:group => 'backend', :name => 'foo-bar').should eql [@guard_foo_bar_backend]
end
it "group name is a symbol" do
subject.guards(:group => :frontend, :name => :'foo-baz').should eql [@guard_foo_baz_frontend]
end
it "returns [] if guard is not found" do
subject.guards(:group => :unknown, :name => :'foo-baz').should eql []
end
end
end end
describe ".groups" do describe ".groups" do