Skip to content

Commit

Permalink
Add bang versions for filter and ignore listener methods
Browse files Browse the repository at this point in the history
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
Andras Tarsoly committed Nov 20, 2012
1 parent eebaeeb commit a0e2607
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 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
14 changes: 14 additions & 0 deletions spec/listen/directory_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,27 @@
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)})
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 eql [%r{\.(?:jpe?g|gif|png)}, %r{\.(?:mp3|ogg|a3c)}]
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
18 changes: 18 additions & 0 deletions spec/listen/multi_listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions spec/support/listeners_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a0e2607

Please sign in to comment.