From dc2ab97d234a5cf85a8cdcdd61614e645addc2ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81my=20Coutable?= Date: Wed, 17 Aug 2011 10:04:42 +0200 Subject: [PATCH] Pass the group in the options hash instead of a new parameter in the Guard::Guard.initialize method! --- lib/guard.rb | 4 ++-- lib/guard/dsl.rb | 3 ++- lib/guard/guard.rb | 4 ++-- spec/guard/dsl_spec.rb | 42 +++++++++++++++++++++--------------------- spec/guard_spec.rb | 24 ++++++++---------------- 5 files changed, 35 insertions(+), 42 deletions(-) diff --git a/lib/guard.rb b/lib/guard.rb index 761bc67..701d428 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -82,11 +82,11 @@ module Guard listener.start end - def add_guard(name, watchers = [], options = {}, group = nil) + def add_guard(name, watchers = [], options = {}) if name.to_sym == :ego UI.deprecation("Guard::Ego is now part of Guard. You can remove it from your Guardfile.") else - guard = get_guard_class(name).new(watchers, options, group) + guard = get_guard_class(name).new(watchers, options) @guards << guard end end diff --git a/lib/guard/dsl.rb b/lib/guard/dsl.rb index 57faefe..cba1b20 100644 --- a/lib/guard/dsl.rb +++ b/lib/guard/dsl.rb @@ -118,7 +118,8 @@ module Guard def guard(name, options = {}, &watch_definition) @watchers = [] watch_definition.call if watch_definition - ::Guard.add_guard(name.to_s.downcase.to_sym, @watchers, options, @current_group || :default) + options.update(:group => (@current_group || :default)) + ::Guard.add_guard(name.to_s.downcase.to_sym, @watchers, options) end def watch(pattern, &action) diff --git a/lib/guard/guard.rb b/lib/guard/guard.rb index b6b081a..8009903 100644 --- a/lib/guard/guard.rb +++ b/lib/guard/guard.rb @@ -3,9 +3,9 @@ module Guard attr_accessor :watchers, :options, :group - def initialize(watchers = [], options = {}, group = nil) + def initialize(watchers = [], options = {}) + @group = options.delete(:group) || :default @watchers, @options = watchers, options - @group ||= :default end # Guardfile template needed inside guard gem diff --git a/spec/guard/dsl_spec.rb b/spec/guard/dsl_spec.rb index 6bbb166..1de89aa 100644 --- a/spec/guard/dsl_spec.rb +++ b/spec/guard/dsl_spec.rb @@ -201,41 +201,41 @@ describe Guard::Dsl do describe "#group" do it "evaluates only the specified string group" do - ::Guard.should_receive(:add_guard).with(:pow, [], {}, :default) - ::Guard.should_receive(:add_guard).with(:test, [], {}, :w) + ::Guard.should_receive(:add_guard).with(:pow, [], { :group => :default }) + ::Guard.should_receive(:add_guard).with(:test, [], { :group => :w }) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => ['w']) end it "evaluates only the specified symbol group" do - ::Guard.should_receive(:add_guard).with(:pow, [], {}, :default) - ::Guard.should_receive(:add_guard).with(:test, [], {}, :w) + ::Guard.should_receive(:add_guard).with(:pow, [], { :group => :default }) + ::Guard.should_receive(:add_guard).with(:test, [], { :group => :w }) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w]) end it "evaluates only the specified groups" do - ::Guard.should_receive(:add_guard).with(:pow, [], {}, :default) - ::Guard.should_receive(:add_guard).with(:rspec, [], {}, :x) - ::Guard.should_receive(:add_guard).with(:ronn, [], {}, :x) - ::Guard.should_receive(:add_guard).with(:less, [], {}, :y) + ::Guard.should_receive(:add_guard).with(:pow, [], { :group => :default }) + ::Guard.should_receive(:add_guard).with(:rspec, [], { :group => :x }) + ::Guard.should_receive(:add_guard).with(:ronn, [], { :group => :x }) + ::Guard.should_receive(:add_guard).with(:less, [], { :group => :y }) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:x, :y]) end it "evaluates always guard outside any group (even when a group is given)" do - ::Guard.should_receive(:add_guard).with(:pow, [], {}, :default) - ::Guard.should_receive(:add_guard).with(:test, [], {}, :w) + ::Guard.should_receive(:add_guard).with(:pow, [], { :group => :default }) + ::Guard.should_receive(:add_guard).with(:test, [], { :group => :w }) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w]) end it "evaluates all groups when no group option is specified" do - ::Guard.should_receive(:add_guard).with(:pow, [], {}, :default) - ::Guard.should_receive(:add_guard).with(:test, [], {}, :w) - ::Guard.should_receive(:add_guard).with(:rspec, [], {}, :x) - ::Guard.should_receive(:add_guard).with(:ronn, [], {}, :x) - ::Guard.should_receive(:add_guard).with(:less, [], {}, :y) + ::Guard.should_receive(:add_guard).with(:pow, [], { :group => :default }) + ::Guard.should_receive(:add_guard).with(:test, [], { :group => :w }) + ::Guard.should_receive(:add_guard).with(:rspec, [], { :group => :x }) + ::Guard.should_receive(:add_guard).with(:ronn, [], { :group => :x }) + ::Guard.should_receive(:add_guard).with(:less, [], { :group => :y }) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) end @@ -243,31 +243,31 @@ describe Guard::Dsl do describe "#guard" do it "loads a guard specified as a quoted string from the DSL" do - ::Guard.should_receive(:add_guard).with(:test, [], {}, :default) + ::Guard.should_receive(:add_guard).with(:test, [], { :group => :default }) subject.evaluate_guardfile(:guardfile_contents => "guard 'test'") end it "loads a guard specified as a double quoted string from the DSL" do - ::Guard.should_receive(:add_guard).with(:test, [], {}, :default) + ::Guard.should_receive(:add_guard).with(:test, [], { :group => :default }) subject.evaluate_guardfile(:guardfile_contents => 'guard "test"') end it "loads a guard specified as a symbol from the DSL" do - ::Guard.should_receive(:add_guard).with(:test, [], {}, :default) + ::Guard.should_receive(:add_guard).with(:test, [], { :group => :default }) subject.evaluate_guardfile(:guardfile_contents => "guard :test") end it "loads a guard specified as a symbol and called with parens from the DSL" do - ::Guard.should_receive(:add_guard).with(:test, [], {}, :default) + ::Guard.should_receive(:add_guard).with(:test, [], { :group => :default }) subject.evaluate_guardfile(:guardfile_contents => "guard(:test)") end it "receives options when specified, from normal arg" do - ::Guard.should_receive(:add_guard).with(:test, [], { :opt_a => 1, :opt_b => 'fancy' }, :default) + ::Guard.should_receive(:add_guard).with(:test, [], { :opt_a => 1, :opt_b => 'fancy', :group => :default }) subject.evaluate_guardfile(:guardfile_contents => "guard 'test', :opt_a => 1, :opt_b => 'fancy'") end @@ -280,7 +280,7 @@ describe Guard::Dsl do watch('c') end" - ::Guard.should_receive(:add_guard).with(:test, anything, {}, :default) do |name, watchers, options| + ::Guard.should_receive(:add_guard).with(:test, anything, { :group => :default }) do |name, watchers, options| watchers.size.should == 2 watchers[0].pattern.should == 'a' watchers[0].action.call.should == proc { 'b' }.call diff --git a/spec/guard_spec.rb b/spec/guard_spec.rb index 461c157..bc62280 100644 --- a/spec/guard_spec.rb +++ b/spec/guard_spec.rb @@ -98,7 +98,7 @@ describe Guard do context "with no watchers given" do it "gives an empty array of watchers" do - @guard_rspec_class.should_receive(:new).with([], {}, nil).and_return(@guard_rspec) + @guard_rspec_class.should_receive(:new).with([], {}).and_return(@guard_rspec) Guard.add_guard(:rspec, []) end @@ -106,7 +106,7 @@ describe Guard do context "with watchers given" do it "give the watchers array" do - @guard_rspec_class.should_receive(:new).with([:foo], {}, nil).and_return(@guard_rspec) + @guard_rspec_class.should_receive(:new).with([:foo], {}).and_return(@guard_rspec) Guard.add_guard(:rspec, [:foo]) end @@ -114,7 +114,7 @@ describe Guard do context "with no options given" do it "gives an empty hash of options" do - @guard_rspec_class.should_receive(:new).with([], {}, nil).and_return(@guard_rspec) + @guard_rspec_class.should_receive(:new).with([], {}).and_return(@guard_rspec) Guard.add_guard(:rspec, [], {}) end @@ -122,17 +122,9 @@ describe Guard do context "with options given" do it "give the options hash" do - @guard_rspec_class.should_receive(:new).with([], { :foo => true }, nil).and_return(@guard_rspec) + @guard_rspec_class.should_receive(:new).with([], { :foo => true, :group => :backend }).and_return(@guard_rspec) - Guard.add_guard(:rspec, [], { :foo => true }) - end - end - - context "with the group :backend given" do - it "initialize the guard and pass it its group" do - @guard_rspec_class.should_receive(:new).with([], {}, :backend).and_return(@guard_rspec) - - Guard.add_guard(:rspec, [], {}, :backend) + Guard.add_guard(:rspec, [], { :foo => true, :group => :backend }) end end end @@ -145,17 +137,17 @@ describe Guard do it "accepts group name as string" do Guard.add_group('backend') - + Guard.groups.should eql [:default, :backend] end it "accepts group name as symbol" do Guard.add_group(:backend) - + Guard.groups.should eql [:default, :backend] end end - + describe ".get_guard_class" do after do [:Classname, :DashedClassName, :Inline].each do |const|