From f1f0f1affbb95c3108286e5ba9bbf26def01234b Mon Sep 17 00:00:00 2001 From: Yossef Mendelssohn Date: Sat, 14 Jun 2008 16:00:24 -0500 Subject: [PATCH] Adding FSEvents::Stream.create, which returns a new FSEvents::Stream object with the stream created. I imagine this will be a more popular interface than FSEvents::Stream.new since it removes the step of creating the stream after initialization. --- lib/fsevents/stream.rb | 8 +++++++ spec/stream_spec.rb | 51 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/fsevents/stream.rb b/lib/fsevents/stream.rb index 60c4bb7..42ec1b2 100644 --- a/lib/fsevents/stream.rb +++ b/lib/fsevents/stream.rb @@ -51,6 +51,14 @@ def startup start end + class << self + def create(*args, &block) + stream = new(*args, &block) + stream.create + stream + end + end + def stop OSX.FSEventStreamStop(stream) end diff --git a/spec/stream_spec.rb b/spec/stream_spec.rb index a3526b7..3725508 100644 --- a/spec/stream_spec.rb +++ b/spec/stream_spec.rb @@ -2,7 +2,7 @@ describe FSEvents::Stream do before :each do - @path = "/tmp" + @path = '/tmp' @stream = FSEvents::Stream.new(@path) {} end @@ -267,6 +267,55 @@ end end + it 'should create' do + FSEvents::Stream.should respond_to(:create) + end + + describe 'when creating' do + before :each do + @other_path = '/other/path' + end + + # This is just here for organization and use of the before block. + # I'd like to ensure that the block is passed to new, but mocha expecation apparently doesn't support that. + # So instead I stub new for some testing and then have something that actually uses new and sees the callback + # is the expected block. + describe do + before :each do + @stream.stubs(:create) + FSEvents::Stream.stubs(:new).returns(@stream) + end + + it 'should accept arguments and a block' do + lambda { FSEvents::Stream.create(@path, @other_path, :flags => 27) {} }.should_not raise_error(ArgumentError) + end + + it 'should initialize a new stream object' do + FSEvents::Stream.expects(:new).returns(@stream) + FSEvents::Stream.create(@path, @other_path, :flags => 27) {} + end + + it 'should pass the arguments to the initialization' do + FSEvents::Stream.expects(:new).with(@path, @other_path, :flags => 27).returns(@stream) + FSEvents::Stream.create(@path, @other_path, :flags => 27) {} + end + + it 'should make the resultant stream object create a stream' do + @stream.expects(:create) + FSEvents::Stream.create(@path, @other_path, :flags => 27) {} + end + + it 'should return the stream object' do + FSEvents::Stream.create.should == @stream + end + end + + it 'should pass the callback block' do + callback = lambda {} + FSEvents::Stream.create(@path, @other_path, :flags => 27, &callback).callback.should == callback + end + end + it 'should schedule itself' do @stream.should respond_to(:schedule) end