Skip to content

Commit

Permalink
Merge pull request guard#68 from tarsolya/override-default-ignore-pat…
Browse files Browse the repository at this point in the history
…terns

Replacing default ignore and filter patterns, instead of adding to them
  • Loading branch information
thibaudgg committed Nov 21, 2012
2 parents eebaeeb + 83537b2 commit c0c3f48
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 3 deletions.
22 changes: 22 additions & 0 deletions lib/listen/directory_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ def ignore(*regexps)
@ignoring_patterns.merge(regexps)
end

# Replaces ignoring patterns in the record.
#
# @example Ignore only these paths
# ignore! %r{^ignored/path/}, /man/
#
# @param [Regexp] regexp a pattern for ignoring paths
#
def ignore!(*regexps)
@ignoring_patterns.replace(regexps)
end

# Adds filtering patterns to the listener.
#
# @example Filter some files
Expand All @@ -99,6 +110,17 @@ def filter(*regexps)
@filtering_patterns.merge(regexps)
end

# Replaces filtering patterns in the listener.
#
# @example Filter only these files
# ignore /\.txt$/, /.*\.zip/
#
# @param [Regexp] regexp a pattern for filtering paths
#
def filter!(*regexps)
@filtering_patterns.replace(regexps)
end

# Returns whether a path should be ignored or not.
#
# @param [String] path the path to test.
Expand Down
22 changes: 22 additions & 0 deletions lib/listen/listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ def ignore(*regexps)
self
end

# Replaces ignoring patterns in the listener.
#
# @param (see Listen::DirectoryRecord#ignore!)
#
# @return [Listen::Listener] the listener
#
def ignore!(*regexps)
@directory_record.ignore!(*regexps)
self
end

# Adds filtering patterns to the listener.
#
# @param (see Listen::DirectoryRecord#filter)
Expand All @@ -104,6 +115,17 @@ def filter(*regexps)
self
end

# Replacing filtering patterns in the listener.
#
# @param (see Listen::DirectoryRecord#filter!)
#
# @return [Listen::Listener] the listener
#
def filter!(*regexps)
@directory_record.filter!(*regexps)
self
end

# Sets the latency for the adapter. This is a helper method
# to simplify changing the latency directly from the listener.
#
Expand Down
22 changes: 22 additions & 0 deletions lib/listen/multi_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ def ignore(*paths)
self
end

# Replaces ignored paths in the listener.
#
# @param (see Listen::DirectoryRecord#ignore!)
#
# @return [Listen::Listener] the listener
#
def ignore!(*paths)
@directories_records.each { |r| r.ignore!(*paths) }
self
end

# Adds file filters to the listener.
#
# @param (see Listen::DirectoryRecord#filter)
Expand All @@ -76,6 +87,17 @@ def filter(*regexps)
self
end

# Replaces file filters in the listener.
#
# @param (see Listen::DirectoryRecord#filter!)
#
# @return [Listen::Listener] the listener
#
def filter!(*regexps)
@directories_records.each { |r| r.filter!(*regexps) }
self
end

# Runs the callback passing it the changes if there are any.
#
# @param (see Listen::DirectoryRecord#fetch_changes)
Expand Down
16 changes: 15 additions & 1 deletion spec/listen/directory_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,33 @@
end

describe '#ignore' do
it 'adds the passed paths to the list of ignoted paths in the record' do
it 'adds the passed paths to the list of ignored paths in the record' do
subject.ignore(%r{^\.old/}, %r{\.pid$})
subject.ignoring_patterns.should include(%r{^\.old/}, %r{\.pid$})
end
end

describe '#ignore!' do
it 'replace the ignored paths in the record' do
subject.ignore!(%r{^\.old/}, %r{\.pid$})
subject.ignoring_patterns.should =~ [%r{^\.old/}, %r{\.pid$}]
end
end

describe '#filter' do
it 'adds the passed regexps to the list of filters that determine the stored paths' do
subject.filter(%r{\.(?:jpe?g|gif|png)}, %r{\.(?:mp3|ogg|a3c)})
subject.filtering_patterns.should include(%r{\.(?:jpe?g|gif|png)}, %r{\.(?:mp3|ogg|a3c)})
end
end

describe '#filter!' do
it 'replaces the passed regexps in the list of filters that determine the stored paths' do
subject.filter!(%r{\.(?:jpe?g|gif|png)}, %r{\.(?:mp3|ogg|a3c)})
subject.filtering_patterns.should =~ [%r{\.(?:mp3|ogg|a3c)}, %r{\.(?:jpe?g|gif|png)}]
end
end

describe '#ignored?' do
before { subject.stub(:relative_to_base) { |path| path } }

Expand Down
14 changes: 14 additions & 0 deletions spec/listen/listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,27 @@
end
end

describe '#ignore!'do
it 'delegates the work to the directory record' do
subject.directory_record.should_receive(:ignore!).with 'some_directory'
subject.ignore! 'some_directory'
end
end

describe '#filter' do
it 'delegates the work to the directory record' do
subject.directory_record.should_receive(:filter).with /\.txt$/
subject.filter /\.txt$/
end
end

describe '#filter!' do
it 'delegates the work to the directory record' do
subject.directory_record.should_receive(:filter!).with /\.txt$/
subject.filter! /\.txt$/
end
end


describe '#on_change' do
let(:directories) { %w{dir1 dir2 dir3} }
Expand Down
20 changes: 19 additions & 1 deletion spec/listen/multi_listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
end
end

describe '#ignore'do
describe '#ignore' do
it 'delegates the work to each directory record' do
subject.directories_records.each do |r|
r.should_receive(:ignore).with 'some_directory'
Expand All @@ -89,6 +89,15 @@
end
end

describe '#ignore!' do
it 'delegates the work to each directory record' do
subject.directories_records.each do |r|
r.should_receive(:ignore!).with 'some_directory'
end
subject.ignore! 'some_directory'
end
end

describe '#filter' do
it 'delegates the work to each directory record' do
subject.directories_records.each do |r|
Expand All @@ -98,6 +107,15 @@
end
end

describe '#filter!' do
it 'delegates the work to each directory record' do
subject.directories_records.each do |r|
r.should_receive(:filter!).with /\.txt$/
end
subject.filter! /\.txt$/
end
end

describe '#on_change' do
let(:directories) { %w{dir1 dir2 dir3} }
let(:changes) { {:modified => [], :added => [], :removed => []} }
Expand Down
14 changes: 13 additions & 1 deletion spec/support/listeners_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,30 @@
end
end

describe '#ignore'do
describe '#ignore' do
it 'returns the same listener to allow chaining' do
subject.ignore('some_directory').should equal subject
end
end

describe '#ignore!' do
it 'returns the same listener to allow chaining' do
subject.ignore!('some_directory').should equal subject
end
end

describe '#filter' do
it 'returns the same listener to allow chaining' do
subject.filter(/\.txt$/).should equal subject
end
end

describe '#filter!' do
it 'returns the same listener to allow chaining' do
subject.filter!(/\.txt$/).should equal subject
end
end

describe '#latency' do
it 'sets the latency to @adapter_options' do
subject.latency(0.7)
Expand Down

0 comments on commit c0c3f48

Please sign in to comment.