From a0e260710cc17ce10bd6a2bdf81b903602c5b924 Mon Sep 17 00:00:00 2001 From: Andras Tarsoly Date: Wed, 21 Nov 2012 00:54:17 +0100 Subject: [PATCH] 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. --- lib/listen/directory_record.rb | 22 ++++++++++++++++++++++ lib/listen/listener.rb | 22 ++++++++++++++++++++++ lib/listen/multi_listener.rb | 22 ++++++++++++++++++++++ spec/listen/directory_record_spec.rb | 14 ++++++++++++++ spec/listen/listener_spec.rb | 14 ++++++++++++++ spec/listen/multi_listener_spec.rb | 18 ++++++++++++++++++ spec/support/listeners_helper.rb | 12 ++++++++++++ 7 files changed, 124 insertions(+) diff --git a/lib/listen/directory_record.rb b/lib/listen/directory_record.rb index 3eb43e31..4dbd4a8c 100644 --- a/lib/listen/directory_record.rb +++ b/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. diff --git a/lib/listen/listener.rb b/lib/listen/listener.rb index 18e2214a..80ec227a 100644 --- a/lib/listen/listener.rb +++ b/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. # diff --git a/lib/listen/multi_listener.rb b/lib/listen/multi_listener.rb index 33f763be..4ff08ca2 100644 --- a/lib/listen/multi_listener.rb +++ b/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) diff --git a/spec/listen/directory_record_spec.rb b/spec/listen/directory_record_spec.rb index c76e9dd8..f6115d74 100644 --- a/spec/listen/directory_record_spec.rb +++ b/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 } } diff --git a/spec/listen/listener_spec.rb b/spec/listen/listener_spec.rb index 0dd07b1f..cf82f1b6 100644 --- a/spec/listen/listener_spec.rb +++ b/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} } diff --git a/spec/listen/multi_listener_spec.rb b/spec/listen/multi_listener_spec.rb index 4dcd24ab..5d1ab918 100644 --- a/spec/listen/multi_listener_spec.rb +++ b/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 => []} } diff --git a/spec/support/listeners_helper.rb b/spec/support/listeners_helper.rb index 125234e4..4e573e36 100644 --- a/spec/support/listeners_helper.rb +++ b/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)