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'
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

View File

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

View File

@ -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?

View File

@ -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

View File

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

View File

@ -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=)