Skip to content

Commit

Permalink
Refactoring FSEvents::Stream initialization to not do too much.
Browse files Browse the repository at this point in the history
This is just the first step, setting attributes in initialization for later methods to use.
  • Loading branch information
ymendel committed Jun 14, 2008
1 parent 70a7ec0 commit eed34f7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/fsevents/stream.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module FSEvents
class Stream
attr_reader :stream
attr_reader :allocator, :context, :paths, :since, :latency, :flags

class StreamError < StandardError; end

Expand All @@ -10,14 +11,15 @@ def initialize(*paths)

paths = Dir.pwd if paths.empty?

allocator = options[:allocator] || OSX::KCFAllocatorDefault
@allocator = options[:allocator] || OSX::KCFAllocatorDefault
callback = options[:callback]
context = options[:context] || nil
paths = [paths].flatten
since = options[:since] || OSX::KFSEventStreamEventIdSinceNow
latency = options[:latency] || 1.0
flags = options[:flags ] || 0
@context = options[:context] || nil
@paths = [paths].flatten
@since = options[:since] || OSX::KFSEventStreamEventIdSinceNow
@latency = options[:latency] || 1.0
@flags = options[:flags ] || 0

paths = @paths
@stream = OSX.FSEventStreamCreate(allocator, callback, context, paths, since, latency, flags)
raise StreamError, 'Unable to create FSEvents stream.' unless @stream
end
Expand Down
71 changes: 71 additions & 0 deletions spec/stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,77 @@
lambda { FSEvents::Stream.new(@path, '/other/path', :flags => 27) }.should_not raise_error
end

describe 'handling options' do
before :each do
@options = {}
[:allocator, :context, :since, :latency, :flags].each do |opt|
@options[opt] = stub(opt.to_s)
end
@other_path = '/other/path'
end

it 'should store the allocator' do
FSEvents::Stream.new(@path, @options).allocator.should == @options[:allocator]
end

it 'should default the allocator to KCFAllocatorDefault' do
@options.delete(:allocator)
FSEvents::Stream.new(@path, @options).allocator.should == OSX::KCFAllocatorDefault
end

it 'should store the context' do
FSEvents::Stream.new(@path, @options).context.should == @options[:context]
end

it 'should default the context to nil' do
@options.delete(:context)
FSEvents::Stream.new(@path, @options).context.should == nil
end

it 'should store the path as an array' do
FSEvents::Stream.new(@path, @options).paths.should == [@path]
end

it 'should store an array of paths as-is' do
FSEvents::Stream.new([@path, @other_path], @options).paths.should == [@path, @other_path]
end

it 'should store multiple paths as an array' do
FSEvents::Stream.new(@path, @other_path, @options).paths.should == [@path, @other_path]
end

it 'should default the path to the present working directory' do
FSEvents::Stream.new(@options).paths.should == [Dir.pwd]
end

it "should store 'since' (event ID)" do
FSEvents::Stream.new(@path, @options).since.should == @options[:since]
end

it "should default 'since' to KFSEventStreamEventIdSinceNow" do
@options.delete(:since)
FSEvents::Stream.new(@path, @options).since.should == OSX::KFSEventStreamEventIdSinceNow
end

it 'should store the latency' do
FSEvents::Stream.new(@path, @options).latency.should == @options[:latency]
end

it 'should default the latency to 1.0' do
@options.delete(:latency)
FSEvents::Stream.new(@path, @options).latency.should == 1.0
end

it 'should store the flags' do
FSEvents::Stream.new(@path, @options).flags.should == @options[:flags]
end

it 'should default the flags to 0' do
@options.delete(:flags)
FSEvents::Stream.new(@path, @options).flags.should == 0
end
end

it 'should create a new stream' do
OSX.expects(:FSEventStreamCreate).returns(@stream)
FSEvents::Stream.new(@path)
Expand Down

0 comments on commit eed34f7

Please sign in to comment.