Permalink
Browse files

Remove Listen.to_each() in favor of using Listen.to() to handle singl…

…e and mulitple directories.
  • Loading branch information...
1 parent 1aa5ebe commit 48d5787020cd604c298bf407d6ff6afab0ee0765 @Maher4Ever Maher4Ever committed Mar 26, 2012
Showing with 63 additions and 39 deletions.
  1. +7 −17 lib/listen.rb
  2. +56 −22 spec/listen_spec.rb
View
@@ -13,9 +13,10 @@ module Adapters
autoload :Polling, 'listen/adapters/polling'
end
- # Listens to filesystem modifications on a single directory.
+ # Listens to filesystem modifications on a either single directory or multiple directories.
#
# @param (see Listen::Listener#new)
+ # @param (see Listen::MultiListener#new)
#
# @yield [modified, added, removed] the changed files
# @yieldparam [Array<String>] modified the list of modified files
@@ -25,23 +26,12 @@ module Adapters
# @return [Listen::Listener] the file listener if no block given
#
def self.to(*args, &block)
- listener = Listener.new(*args, &block)
- block ? listener.start : listener
- end
+ listener = if args.length == 1 || ! args[1].is_a?(String)
+ Listener.new(*args, &block)
+ else
+ MultiListener.new(*args, &block)
+ end
- # Listens to filesystem modifications on multiple directories.
- #
- # @param (see Listen::MultiListener#new)
- #
- # @yield [modified, added, removed] the changed files
- # @yieldparam [Array<String>] modified the list of modified files
- # @yieldparam [Array<String>] added the list of added files
- # @yieldparam [Array<String>] removed the list of removed files
- #
- # @return [Listen::MultiListener] the file listener if no block given
- #
- def self.to_each(*args, &block)
- listener = MultiListener.new(*args, &block)
block ? listener.start : listener
end
View
@@ -2,37 +2,71 @@
describe Listen do
describe '#to' do
- let(:listener) { mock(Listen::Listener) }
- before { Listen::Listener.stub(:new).with("/path", :filter => '**/*') { listener } }
+ context 'with one path to listen to' do
+ let(:listener) { mock(Listen::Listener) }
+ let(:listener_class) { Listen::Listener }
- context 'with a block' do
- it "returns a new listener created with the passed params" do
- Listen.to('/path', :filter => '**/*').should eq listener
+ before { listener_class.stub(:new => listener) }
+
+ context 'without options' do
+ it 'creates an instance of Listner' do
+ listener_class.should_receive(:new).with('/path')
+ described_class.to('/path')
+ end
end
- end
- context 'without a block' do
- it 'starts a new listener after creating it with the passed params' do
- listener.should_receive(:start)
- Listen.to('/path', :filter => '**/*') { |modified, added, removed| }
+ context 'with options' do
+ it 'creates an instance of Listner with the passed params' do
+ listener_class.should_receive(:new).with('/path', :filter => '**/*')
+ described_class.to('/path', :filter => '**/*')
+ end
end
- end
- end
- describe "#to_each" do
- let(:multi_listener) { mock(Listen::MultiListener) }
- before { Listen::MultiListener.stub(:new).with('path1', 'path2', :filter => '**/*') { multi_listener } }
+ context 'without a block' do
+ it 'returns the listener' do
+ described_class.to('/path', :filter => '**/*').should eq listener
+ end
+ end
- context 'with a block' do
- it "returns a new listener created with the passed params" do
- Listen.to_each('path1', 'path2', :filter => '**/*').should eq multi_listener
+ context 'with a block' do
+ it 'starts the listner after creating it' do
+ listener.should_receive(:start)
+ described_class.to('/path', :filter => '**/*') { |modified, added, removed| }
+ end
end
end
- context 'without a block' do
- it 'starts a new listener after creating it with the passed params' do
- multi_listener.should_receive(:start)
- Listen.to_each('path1', 'path2', :filter => '**/*') { |modified, added, removed| }
+ context 'with multiple paths to listen to' do
+ let(:multi_listener) { mock(Listen::MultiListener) }
+ let(:multi_listener_class) { Listen::MultiListener }
+
+ before { multi_listener_class.stub(:new => multi_listener) }
+
+ context 'without options' do
+ it 'creates an instance of MultiListner' do
+ multi_listener_class.should_receive(:new).with('path1', 'path2')
+ described_class.to('path1', 'path2')
+ end
+ end
+
+ context 'with options' do
+ it 'creates an instance of MultiListner with the passed params' do
+ multi_listener_class.should_receive(:new).with('path1', 'path2', :filter => '**/*')
+ described_class.to('path1', 'path2', :filter => '**/*')
+ end
+ end
+
+ context 'without a block' do
+ it 'returns a MultiListener instance created with the passed params' do
+ described_class.to('path1', 'path2', :filter => '**/*').should eq multi_listener
+ end
+ end
+
+ context 'with a block' do
+ it 'starts a MultiListener instance after creating it with the passed params' do
+ multi_listener.should_receive(:start)
+ described_class.to('path1', 'path2', :filter => '**/*') { |modified, added, removed| }
+ end
end
end
end

2 comments on commit 48d5787

Owner

rymai replied Mar 26, 2012

I like that!

Owner

thibaudgg replied Mar 26, 2012

Great!

Please sign in to comment.