diff --git a/lib/guard/listener.rb b/lib/guard/listener.rb index 669f163..b06aad5 100644 --- a/lib/guard/listener.rb +++ b/lib/guard/listener.rb @@ -9,7 +9,6 @@ module Guard autoload :Polling, 'guard/listeners/polling' class Listener - attr_reader :last_event, :sha1_checksums_hash, :directory, :callback def self.select_and_init(*a) if mac? && Darwin.usable? @@ -25,14 +24,14 @@ module Guard end def initialize(directory=Dir.pwd, options={}) - @directory = directory.to_s + @directory = directory.to_s @sha1_checksums_hash = {} - @relativate_paths = options.fetch(:relativate_paths, true) + @relativize_paths = options.fetch(:relativize_paths, true) update_last_event end def start - watch directory + watch(@directory) end def stop @@ -66,16 +65,15 @@ module Guard end # scopes all given paths to the current #directory - def relativate_paths(paths) - return paths unless relativate_paths? + def relativize_paths(paths) + return paths unless relativize_paths? paths.map do |path| - path.gsub(%r~^#{directory}/~, '') + path.gsub(%r{^#{@directory}/}, '') end end - attr_writer :relativate_paths - def relativate_paths? - !!@relativate_paths + def relativize_paths? + !!@relativize_paths end private @@ -88,9 +86,9 @@ module Guard # Depending on the filesystem, mtime is probably only precise to the second, so round # both values down to the second for the comparison. def file_modified?(path) - if File.mtime(path).to_i == last_event.to_i + if File.mtime(path).to_i == @last_event.to_i file_content_modified?(path, sha1_checksum(path)) - elsif File.mtime(path).to_i > last_event.to_i + elsif File.mtime(path).to_i > @last_event.to_i set_sha1_checksums_hash(path, sha1_checksum(path)) true end @@ -99,7 +97,7 @@ module Guard end def file_content_modified?(path, sha1_checksum) - if sha1_checksums_hash[path] != sha1_checksum + if @sha1_checksums_hash[path] != sha1_checksum set_sha1_checksums_hash(path, sha1_checksum) true else diff --git a/lib/guard/listeners/darwin.rb b/lib/guard/listeners/darwin.rb index fac8093..a064e72 100644 --- a/lib/guard/listeners/darwin.rb +++ b/lib/guard/listeners/darwin.rb @@ -1,6 +1,5 @@ module Guard class Darwin < Listener - attr_reader :fsevent def initialize(*) super diff --git a/lib/guard/listeners/linux.rb b/lib/guard/listeners/linux.rb index 5622a31..79a8ab8 100644 --- a/lib/guard/listeners/linux.rb +++ b/lib/guard/listeners/linux.rb @@ -1,6 +1,5 @@ module Guard class Linux < Listener - attr_reader :inotify, :files, :latency def initialize(*) super @@ -19,7 +18,7 @@ module Guard def stop super @stop = true - sleep latency + sleep(@latency) end def self.usable? @@ -62,8 +61,8 @@ module Guard if RbConfig::CONFIG['build'] =~ /java/ || IO.select([inotify.to_io], [], [], latency) break if @stop - sleep latency inotify.process + sleep(@latency) files = modified_files(@files.shift(@files.size).map { |f| File.dirname(f) }.uniq) @callback.call(files) unless files.empty? diff --git a/lib/guard/listeners/polling.rb b/lib/guard/listeners/polling.rb index cbaed22..1c9b0db 100644 --- a/lib/guard/listeners/polling.rb +++ b/lib/guard/listeners/polling.rb @@ -1,6 +1,5 @@ module Guard class Polling < Listener - attr_reader :latency def initialize(*) super @@ -24,8 +23,8 @@ module Guard until @stop start = Time.now.to_f files = modified_files([Dir.pwd + '/'], :all => true) - nap_time = latency - (Time.now.to_f - start) @callback.call(files) unless files.empty? + nap_time = @latency - (Time.now.to_f - start) sleep(nap_time) if nap_time > 0 end end diff --git a/lib/guard/listeners/windows.rb b/lib/guard/listeners/windows.rb index 4f5d6c4..7f525e1 100644 --- a/lib/guard/listeners/windows.rb +++ b/lib/guard/listeners/windows.rb @@ -1,6 +1,5 @@ module Guard class Windows < Listener - attr_reader :fchange def initialize(*) super diff --git a/spec/guard/listener_spec.rb b/spec/guard/listener_spec.rb index 68c9dac..6eb675a 100644 --- a/spec/guard/listener_spec.rb +++ b/spec/guard/listener_spec.rb @@ -46,19 +46,22 @@ describe Guard::Listener do end end - describe "#relativate_paths" do + describe "#relativize_paths" do subject { described_class.new('/tmp') } before :each do @paths = %w( /tmp/a /tmp/a/b /tmp/a.b/c.d ) end - it "should relativate paths to the configured directory" do - subject.relativate_paths(@paths).should =~ %w( a a/b a.b/c.d ) + it "should relativize paths to the configured directory" do + subject.relativize_paths(@paths).should =~ %w( a a/b a.b/c.d ) end - - it "can be disabled" do - subject.relativate_paths = false - subject.relativate_paths(@paths).should == @paths + + context "when set to false" do + subject { described_class.new('/tmp', :relativize_paths => false) } + + it "can be disabled" do + subject.relativize_paths(@paths).should eql @paths + end end end @@ -68,7 +71,7 @@ describe Guard::Listener do it "updates the last event to the current time" do time = Time.now subject.update_last_event - subject.last_event.to_i.should >= time.to_i + subject.instance_variable_get(:@last_event).to_i.should >= time.to_i end end @@ -129,7 +132,7 @@ describe Guard::Listener do context "unspecified" do subject { described_class.new } it "defaults to Dir.pwd" do - subject.directory.should == Dir.pwd + subject.instance_variable_get(:@directory).should eql Dir.pwd end it "can be not changed" do subject.should_not respond_to(:directory=) @@ -142,7 +145,7 @@ describe Guard::Listener do end subject { described_class.new @wd } it "can be inspected" do - subject.directory.should == @wd.to_s + subject.instance_variable_get(:@directory).should eql @wd.to_s end it "can be not changed" do subject.should_not respond_to(:directory=)