On a guard's initialization, pass the group in which it's been declared in the Guardfile, pass group's name on Guard.add_guard and thus in the Dsl

This commit is contained in:
Rémy Coutable 2011-08-17 00:36:02 +02:00
parent e6dc507de7
commit 6f7ce6feb7
3 changed files with 32 additions and 25 deletions

View File

@ -106,13 +106,19 @@ module Guard
def group(name, &guard_definition) def group(name, &guard_definition)
@groups = @@options[:group] || [] @groups = @@options[:group] || []
guard_definition.call if guard_definition && (@groups.empty? || @groups.map(&:to_sym).include?(name.to_sym)) name = name.to_sym
if guard_definition && (@groups.empty? || @groups.map(&:to_sym).include?(name))
@current_group = name
guard_definition.call
@current_group = nil
end
end end
def guard(name, options = {}, &watch_definition) def guard(name, options = {}, &watch_definition)
@watchers = [] @watchers = []
watch_definition.call if watch_definition watch_definition.call if watch_definition
::Guard.add_guard(name.to_sym, @watchers, @guard_options) ::Guard.add_guard(name.to_s.downcase.to_sym, @watchers, options, @current_group || :default)
end end
def watch(pattern, &action) def watch(pattern, &action)

View File

@ -1,10 +1,11 @@
module Guard module Guard
class Guard class Guard
attr_accessor :watchers, :options attr_accessor :watchers, :options, :group
def initialize(watchers = [], options = {}) def initialize(watchers = [], options = {}, group = nil)
@watchers, @options = watchers, options @watchers, @options = watchers, options
@group ||= :default
end end
# Guardfile template needed inside guard gem # Guardfile template needed inside guard gem

View File

@ -201,41 +201,41 @@ describe Guard::Dsl do
describe "#group" do describe "#group" do
it "evaluates only the specified string group" do it "evaluates only the specified string group" do
::Guard.should_receive(:add_guard).with(:pow, [], {}) ::Guard.should_receive(:add_guard).with(:pow, [], {}, :default)
::Guard.should_receive(:add_guard).with(:test, [], {}) ::Guard.should_receive(:add_guard).with(:test, [], {}, :w)
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => ['w']) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => ['w'])
end end
it "evaluates only the specified symbol group" do it "evaluates only the specified symbol group" do
::Guard.should_receive(:add_guard).with(:pow, [], {}) ::Guard.should_receive(:add_guard).with(:pow, [], {}, :default)
::Guard.should_receive(:add_guard).with(:test, [], {}) ::Guard.should_receive(:add_guard).with(:test, [], {}, :w)
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w]) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w])
end end
it "evaluates only the specified groups" do it "evaluates only the specified groups" do
::Guard.should_receive(:add_guard).with(:pow, [], {}) ::Guard.should_receive(:add_guard).with(:pow, [], {}, :default)
::Guard.should_receive(:add_guard).with(:rspec, [], {}) ::Guard.should_receive(:add_guard).with(:rspec, [], {}, :x)
::Guard.should_receive(:add_guard).with(:ronn, [], {}) ::Guard.should_receive(:add_guard).with(:ronn, [], {}, :x)
::Guard.should_receive(:add_guard).with(:less, [], {}) ::Guard.should_receive(:add_guard).with(:less, [], {}, :y)
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:x, :y]) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:x, :y])
end end
it "evaluates always guard outside any group (even when a group is given)" do it "evaluates always guard outside any group (even when a group is given)" do
::Guard.should_receive(:add_guard).with(:pow, [], {}) ::Guard.should_receive(:add_guard).with(:pow, [], {}, :default)
::Guard.should_receive(:add_guard).with(:test, [], {}) ::Guard.should_receive(:add_guard).with(:test, [], {}, :w)
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w]) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w])
end end
it "evaluates all groups when no group option is specified" do it "evaluates all groups when no group option is specified" do
::Guard.should_receive(:add_guard).with(:pow, [], {}) ::Guard.should_receive(:add_guard).with(:pow, [], {}, :default)
::Guard.should_receive(:add_guard).with(:test, [], {}) ::Guard.should_receive(:add_guard).with(:test, [], {}, :w)
::Guard.should_receive(:add_guard).with(:rspec, [], {}) ::Guard.should_receive(:add_guard).with(:rspec, [], {}, :x)
::Guard.should_receive(:add_guard).with(:ronn, [], {}) ::Guard.should_receive(:add_guard).with(:ronn, [], {}, :x)
::Guard.should_receive(:add_guard).with(:less, [], {}) ::Guard.should_receive(:add_guard).with(:less, [], {}, :y)
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string)
end end
@ -243,31 +243,31 @@ describe Guard::Dsl do
describe "#guard" do describe "#guard" do
it "loads a guard specified as a quoted string from the DSL" do it "loads a guard specified as a quoted string from the DSL" do
::Guard.should_receive(:add_guard).with(:test, [], {}) ::Guard.should_receive(:add_guard).with(:test, [], {}, :default)
subject.evaluate_guardfile(:guardfile_contents => "guard 'test'") subject.evaluate_guardfile(:guardfile_contents => "guard 'test'")
end end
it "loads a guard specified as a double quoted string from the DSL" do it "loads a guard specified as a double quoted string from the DSL" do
::Guard.should_receive(:add_guard).with(:test, [], {}) ::Guard.should_receive(:add_guard).with(:test, [], {}, :default)
subject.evaluate_guardfile(:guardfile_contents => 'guard "test"') subject.evaluate_guardfile(:guardfile_contents => 'guard "test"')
end end
it "loads a guard specified as a symbol from the DSL" do it "loads a guard specified as a symbol from the DSL" do
::Guard.should_receive(:add_guard).with(:test, [], {}) ::Guard.should_receive(:add_guard).with(:test, [], {}, :default)
subject.evaluate_guardfile(:guardfile_contents => "guard :test") subject.evaluate_guardfile(:guardfile_contents => "guard :test")
end end
it "loads a guard specified as a symbol and called with parens from the DSL" do it "loads a guard specified as a symbol and called with parens from the DSL" do
::Guard.should_receive(:add_guard).with(:test, [], {}) ::Guard.should_receive(:add_guard).with(:test, [], {}, :default)
subject.evaluate_guardfile(:guardfile_contents => "guard(:test)") subject.evaluate_guardfile(:guardfile_contents => "guard(:test)")
end end
it "receives options when specified, from normal arg" do it "receives options when specified, from normal arg" do
::Guard.should_receive(:add_guard).with(:test, [], { :opt_a => 1, :opt_b => 'fancy' }) ::Guard.should_receive(:add_guard).with(:test, [], { :opt_a => 1, :opt_b => 'fancy' }, :default)
subject.evaluate_guardfile(:guardfile_contents => "guard 'test', :opt_a => 1, :opt_b => 'fancy'") subject.evaluate_guardfile(:guardfile_contents => "guard 'test', :opt_a => 1, :opt_b => 'fancy'")
end end
@ -280,7 +280,7 @@ describe Guard::Dsl do
watch('c') watch('c')
end" end"
::Guard.should_receive(:add_guard).with(:test, anything, {}) do |name, watchers, options| ::Guard.should_receive(:add_guard).with(:test, anything, {}, :default) do |name, watchers, options|
watchers.size.should == 2 watchers.size.should == 2
watchers[0].pattern.should == 'a' watchers[0].pattern.should == 'a'
watchers[0].action.call.should == proc { 'b' }.call watchers[0].action.call.should == proc { 'b' }.call