Skip to content
Browse files

Switched the parameter to be added to the guard instead of the watcher.

  • Loading branch information...
1 parent a274fb1 commit bdfdf45325f352e5b6bd7dc7fbf69f8dfcf53eb6 @earlonrails committed Oct 10, 2011
Showing with 34 additions and 32 deletions.
  1. +4 −3 lib/guard/guard.rb
  2. +6 −6 lib/guard/watcher.rb
  3. +24 −23 spec/guard/watcher_spec.rb
View
7 lib/guard/guard.rb
@@ -30,16 +30,17 @@ module Guard
class Guard
include Hook
- attr_accessor :watchers, :options, :group
+ attr_accessor :watchers, :options, :group, :any_return
# Initialize a Guard.
#
# @param [Array<Guard::Watcher>] watchers the Guard file watchers
# @param [Hash] options the custom Guard options
+ # @param [Boolean] any_return allow the user to define return when using a block with a watcher
#
- def initialize(watchers = [], options = {})
+ def initialize(watchers = [], options = {}, any_return = false)
@rymai
rymai added a note Oct 10, 2011

You should just use options for that, so there would be no changes here. Just a new documentation item above maybe.

@earlonrails
Owner
earlonrails added a note Oct 10, 2011

How is this ?

attr_accessor :watchers, :options, :group, :any_return

# Initialize a Guard.
#
# @param [Array<Guard::Watcher>] watchers the Guard file watchers
# @param [Hash] options the custom Guard options
#
def initialize(watchers = [], options = {})
  @group = options[:group] ? options.delete(:group).to_sym : :default
  @watchers, @options, @any_return = watchers, options, options[:any_return]
end

** Nevermind I figured it out
You don't need attr_accessor for it cause we already have one for options.

@rymai
rymai added a note Oct 10, 2011

Exactly! The last version in the pull-request is perfect (except for the useless accessor in Watcher).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@group = options[:group] ? options.delete(:group).to_sym : :default
- @watchers, @options = watchers, options
+ @watchers, @options, @any_return = watchers, options, any_return
end
# Initialize the Guard. This will copy the Guardfile template inside the Guard gem.
View
12 lib/guard/watcher.rb
@@ -12,10 +12,9 @@ class Watcher
#
# @param [String, Regexp] pattern the pattern to be watched by the guard
# @param [Block] action the action to execute before passing the result to the Guard
- # @param [Boolean] any_return allow the user to define return when using a block
#
- def initialize(pattern, action = nil, any_return = false)
- @pattern, @action, @any_return = pattern, action, any_return
+ def initialize(pattern, action = nil)
+ @pattern, @action = pattern, action
@@warning_printed ||= false
# deprecation warning
@@ -40,15 +39,16 @@ def initialize(pattern, action = nil, any_return = false)
# @param [Guard::Guard] guard the guard which watchers are used
# @param [Array<String>] files the changed files
# @return [Array<Object>] the matched watcher response
+ #
def self.match_files(guard, files)
guard.watchers.inject([]) do |paths, watcher|
files.each do |file|
if matches = watcher.match_file?(file)
if watcher.action
result = watcher.call_action(matches)
- if watcher.any_return
+ if guard.any_return
@rymai
rymai added a note Oct 10, 2011

And use guard.options[:any_return] here (and just below).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
paths << result
- elsif result.respond_to?(:empty?) && ! result.empty?
+ elsif result.respond_to?(:empty?) && !result.empty?
paths << Array(result)
end
else
@@ -57,7 +57,7 @@ def self.match_files(guard, files)
end
end
- watcher.any_return ? paths : paths.flatten.map{|p| p.to_s}
+ guard.any_return ? paths : paths.flatten.map{ |p| p.to_s }
end
end
View
47 spec/guard/watcher_spec.rb
@@ -46,10 +46,11 @@
end
describe ".match_files" do
- before(:all) { @guard = Guard::Guard.new
- @guard_any_return = Guard::Guard.new
- @guard_any_return.any_return = true
- }
+ before(:all) do
+ @guard = Guard::Guard.new
+ @guard_any_return = Guard::Guard.new
+ @guard_any_return.any_return = true
+ end
context "with a watcher without action" do
context "that is a regex pattern" do
@@ -119,29 +120,29 @@
end
it "returns a single file specified within the action" do
- described_class.match_files(@guard, ['spec_helper.rb']).class.should == Array
- described_class.match_files(@guard, ['spec_helper.rb']).empty?.should == false
+ described_class.match_files(@guard_any_return, ['spec_helper.rb']).class.should == Array
+ described_class.match_files(@guard_any_return, ['spec_helper.rb']).empty?.should == false
end
it "returns multiple files specified within the action" do
- described_class.match_files(@guard, ['hash.rb']).should == [{:foo => 'bar'}]
+ described_class.match_files(@guard_any_return, ['hash.rb']).should == [{:foo => 'bar'}]
end
it "returns multiple files by combining the results of different actions" do
- described_class.match_files(@guard, ['spec_helper.rb', 'array.rb']).should == ['spec', ['foo', 'bar']]
+ described_class.match_files(@guard_any_return, ['spec_helper.rb', 'array.rb']).should == ['spec', ['foo', 'bar']]
end
it "returns the evaluated addition argument in an array" do
- described_class.match_files(@guard, ['addition.rb']).class.should == Array
- described_class.match_files(@guard, ['addition.rb'])[0].should == 2
+ described_class.match_files(@guard_any_return, ['addition.rb']).class.should == Array
+ described_class.match_files(@guard_any_return, ['addition.rb'])[0].should == 2
end
it "returns nothing if the action response is empty string" do
- described_class.match_files(@guard, ['blank.rb']).should == ['']
+ described_class.match_files(@guard_any_return, ['blank.rb']).should == ['']
end
it "returns nothing if the action returns empty string" do
- described_class.match_files(@guard, ['uptime.rb']).should == ['']
+ described_class.match_files(@guard_any_return, ['uptime.rb']).should == ['']
end
end
@@ -150,7 +151,7 @@
@guard.watchers = [
described_class.new(%r{lib/(.*)\.rb}, lambda { |m| "spec/#{m[1]}_spec.rb" }),
described_class.new(/addition(.*)\.rb/, lambda { |m| 1 + 1 }),
- described_class.new('hash.rb', lambda { Hash[:foo, 'bar'] }),
+ described_class.new('hash.rb', lambda { |m| Hash[:foo, 'bar'] }),
described_class.new(/array(.*)\.rb/, lambda { |m| ['foo', 'bar'] }),
described_class.new(/blank(.*)\.rb/, lambda { |m| '' }),
described_class.new(/uptime(.*)\.rb/, lambda { |m| `uptime > /dev/null` })
@@ -184,49 +185,49 @@
context "with a watcher action that takes a parameter for a watcher that matches information objects" do
before(:all) do
- @guard.watchers = [
+ @guard_any_return.watchers = [
described_class.new(%r{lib/(.*)\.rb}, lambda { |m| "spec/#{m[1]}_spec.rb" }),
described_class.new(/addition(.*)\.rb/, lambda { |m| (1 + 1).to_s + m[0] }),
- described_class.new('hash.rb', lambda {|m| Hash[:foo, 'bar', :file_name, m[0]] }),
+ described_class.new('hash.rb', lambda { |m| Hash[:foo, 'bar', :file_name, m[0]] }),
described_class.new(/array(.*)\.rb/, lambda { |m| ['foo', 'bar', m[0]] }),
described_class.new(/blank(.*)\.rb/, lambda { |m| '' }),
described_class.new(/uptime(.*)\.rb/, lambda { |m| `uptime > /dev/null` })
]
end
it "returns a substituted single file specified within the action" do
- described_class.match_files(@guard, ['lib/my_wonderful_lib.rb']).should == ['spec/my_wonderful_lib_spec.rb']
+ described_class.match_files(@guard_any_return, ['lib/my_wonderful_lib.rb']).should == ['spec/my_wonderful_lib_spec.rb']
end
it "returns a hash specified within the action" do
- described_class.match_files(@guard, ['hash.rb']).should == [{:foo => 'bar', :file_name => 'hash.rb'}]
+ described_class.match_files(@guard_any_return, ['hash.rb']).should == [{:foo => 'bar', :file_name => 'hash.rb'}]
end
it "returns multiple files by combining the results of different actions" do
- described_class.match_files(@guard, ['lib/my_wonderful_lib.rb', 'array.rb']).should == ['spec/my_wonderful_lib_spec.rb', ['foo', 'bar', "array.rb"]]
+ described_class.match_files(@guard_any_return, ['lib/my_wonderful_lib.rb', 'array.rb']).should == ['spec/my_wonderful_lib_spec.rb', ['foo', 'bar', "array.rb"]]
end
it "returns the evaluated addition argument + the path" do
- described_class.match_files(@guard, ['addition.rb']).should == ["2addition.rb"]
+ described_class.match_files(@guard_any_return, ['addition.rb']).should == ["2addition.rb"]
end
it "returns nothing if the action response is empty string" do
- described_class.match_files(@guard, ['blank.rb']).should == ['']
+ described_class.match_files(@guard_any_return, ['blank.rb']).should == ['']
end
it "returns nothing if the action returns empty string" do
- described_class.match_files(@guard, ['uptime.rb']).should == ['']
+ described_class.match_files(@guard_any_return, ['uptime.rb']).should == ['']
end
end
context "with an exception that is raised" do
before(:all) { @guard.watchers = [described_class.new('evil.rb', lambda { raise "EVIL" })] }
it "displays the error and backtrace" do
- Guard::UI.should_receive(:error) { |msg|
+ Guard::UI.should_receive(:error) do |msg|
msg.should include("Problem with watch action!")
msg.should include("EVIL")
- }
+ end
described_class.match_files(@guard, ['evil.rb'])
end

1 comment on commit bdfdf45

@rymai
rymai commented on bdfdf45 Oct 10, 2011

Other than my 2 comments, well done for all the little fix we pointed out before! :)

Please sign in to comment.
Something went wrong with that request. Please try again.