Don't add attr_reader and attr_writer when unecessary

Rename Guard::Listener#relativate_paths to Guard::Listener#relativize_paths
This commit is contained in:
Rémy Coutable 2011-07-21 01:29:05 +02:00
parent 8a1ca41626
commit 802d134165
6 changed files with 27 additions and 30 deletions

View File

@ -9,7 +9,6 @@ module Guard
autoload :Polling, 'guard/listeners/polling' autoload :Polling, 'guard/listeners/polling'
class Listener class Listener
attr_reader :last_event, :sha1_checksums_hash, :directory, :callback
def self.select_and_init(*a) def self.select_and_init(*a)
if mac? && Darwin.usable? if mac? && Darwin.usable?
@ -25,14 +24,14 @@ module Guard
end end
def initialize(directory=Dir.pwd, options={}) def initialize(directory=Dir.pwd, options={})
@directory = directory.to_s @directory = directory.to_s
@sha1_checksums_hash = {} @sha1_checksums_hash = {}
@relativate_paths = options.fetch(:relativate_paths, true) @relativize_paths = options.fetch(:relativize_paths, true)
update_last_event update_last_event
end end
def start def start
watch directory watch(@directory)
end end
def stop def stop
@ -66,16 +65,15 @@ module Guard
end end
# scopes all given paths to the current #directory # scopes all given paths to the current #directory
def relativate_paths(paths) def relativize_paths(paths)
return paths unless relativate_paths? return paths unless relativize_paths?
paths.map do |path| paths.map do |path|
path.gsub(%r~^#{directory}/~, '') path.gsub(%r{^#{@directory}/}, '')
end end
end end
attr_writer :relativate_paths def relativize_paths?
def relativate_paths? !!@relativize_paths
!!@relativate_paths
end end
private private
@ -88,9 +86,9 @@ module Guard
# Depending on the filesystem, mtime is probably only precise to the second, so round # Depending on the filesystem, mtime is probably only precise to the second, so round
# both values down to the second for the comparison. # both values down to the second for the comparison.
def file_modified?(path) 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)) 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)) set_sha1_checksums_hash(path, sha1_checksum(path))
true true
end end
@ -99,7 +97,7 @@ module Guard
end end
def file_content_modified?(path, sha1_checksum) 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) set_sha1_checksums_hash(path, sha1_checksum)
true true
else else

View File

@ -1,6 +1,5 @@
module Guard module Guard
class Darwin < Listener class Darwin < Listener
attr_reader :fsevent
def initialize(*) def initialize(*)
super super

View File

@ -1,6 +1,5 @@
module Guard module Guard
class Linux < Listener class Linux < Listener
attr_reader :inotify, :files, :latency
def initialize(*) def initialize(*)
super super
@ -19,7 +18,7 @@ module Guard
def stop def stop
super super
@stop = true @stop = true
sleep latency sleep(@latency)
end end
def self.usable? def self.usable?
@ -62,8 +61,8 @@ module Guard
if RbConfig::CONFIG['build'] =~ /java/ || IO.select([inotify.to_io], [], [], latency) if RbConfig::CONFIG['build'] =~ /java/ || IO.select([inotify.to_io], [], [], latency)
break if @stop break if @stop
sleep latency
inotify.process inotify.process
sleep(@latency)
files = modified_files(@files.shift(@files.size).map { |f| File.dirname(f) }.uniq) files = modified_files(@files.shift(@files.size).map { |f| File.dirname(f) }.uniq)
@callback.call(files) unless files.empty? @callback.call(files) unless files.empty?

View File

@ -1,6 +1,5 @@
module Guard module Guard
class Polling < Listener class Polling < Listener
attr_reader :latency
def initialize(*) def initialize(*)
super super
@ -24,8 +23,8 @@ module Guard
until @stop until @stop
start = Time.now.to_f start = Time.now.to_f
files = modified_files([Dir.pwd + '/'], :all => true) files = modified_files([Dir.pwd + '/'], :all => true)
nap_time = latency - (Time.now.to_f - start)
@callback.call(files) unless files.empty? @callback.call(files) unless files.empty?
nap_time = @latency - (Time.now.to_f - start)
sleep(nap_time) if nap_time > 0 sleep(nap_time) if nap_time > 0
end end
end end

View File

@ -1,6 +1,5 @@
module Guard module Guard
class Windows < Listener class Windows < Listener
attr_reader :fchange
def initialize(*) def initialize(*)
super super

View File

@ -46,19 +46,22 @@ describe Guard::Listener do
end end
end end
describe "#relativate_paths" do describe "#relativize_paths" do
subject { described_class.new('/tmp') } subject { described_class.new('/tmp') }
before :each do before :each do
@paths = %w( /tmp/a /tmp/a/b /tmp/a.b/c.d ) @paths = %w( /tmp/a /tmp/a/b /tmp/a.b/c.d )
end end
it "should relativate paths to the configured directory" do it "should relativize paths to the configured directory" do
subject.relativate_paths(@paths).should =~ %w( a a/b a.b/c.d ) subject.relativize_paths(@paths).should =~ %w( a a/b a.b/c.d )
end end
it "can be disabled" do context "when set to false" do
subject.relativate_paths = false subject { described_class.new('/tmp', :relativize_paths => false) }
subject.relativate_paths(@paths).should == @paths
it "can be disabled" do
subject.relativize_paths(@paths).should eql @paths
end
end end
end end
@ -68,7 +71,7 @@ describe Guard::Listener do
it "updates the last event to the current time" do it "updates the last event to the current time" do
time = Time.now time = Time.now
subject.update_last_event 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
end end
@ -129,7 +132,7 @@ describe Guard::Listener do
context "unspecified" do context "unspecified" do
subject { described_class.new } subject { described_class.new }
it "defaults to Dir.pwd" do it "defaults to Dir.pwd" do
subject.directory.should == Dir.pwd subject.instance_variable_get(:@directory).should eql Dir.pwd
end end
it "can be not changed" do it "can be not changed" do
subject.should_not respond_to(:directory=) subject.should_not respond_to(:directory=)
@ -142,7 +145,7 @@ describe Guard::Listener do
end end
subject { described_class.new @wd } subject { described_class.new @wd }
it "can be inspected" do it "can be inspected" do
subject.directory.should == @wd.to_s subject.instance_variable_get(:@directory).should eql @wd.to_s
end end
it "can be not changed" do it "can be not changed" do
subject.should_not respond_to(:directory=) subject.should_not respond_to(:directory=)