Skip to content
Browse files

Add bang versions for filter and ignore listener methods

The bang versions of these methods replaces the content of the file
ignoring and filtering pattern lists, instead of adding to them.
  • Loading branch information...
1 parent eebaeeb commit a0e260710cc17ce10bd6a2bdf81b903602c5b924 Andras Tarsoly committed
View
22 lib/listen/directory_record.rb
@@ -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
@@ -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.
View
22 lib/listen/listener.rb
@@ -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)
@@ -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.
#
View
22 lib/listen/multi_listener.rb
@@ -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)
@@ -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)
View
14 spec/listen/directory_record_spec.rb
@@ -47,6 +47,13 @@
end
end
+ describe '#ignore!' do
+ it 'replace the ignored paths in the record' do
+ subject.ignore!(%r{^\.old/}, %r{\.pid$})
+ subject.ignoring_patterns.should eql [%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)})
@@ -54,6 +61,13 @@
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 eql [%r{\.(?:jpe?g|gif|png)}, %r{\.(?:mp3|ogg|a3c)}]
+ end
+ end
+
describe '#ignored?' do
before { subject.stub(:relative_to_base) { |path| path } }
View
14 spec/listen/listener_spec.rb
@@ -84,6 +84,13 @@
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$/
@@ -91,6 +98,13 @@
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} }
View
18 spec/listen/multi_listener_spec.rb
@@ -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|
@@ -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 => []} }
View
12 spec/support/listeners_helper.rb
@@ -87,12 +87,24 @@
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)

0 comments on commit a0e2607

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