From e4477d506278e067d7988f04f79d416c395fa412 Mon Sep 17 00:00:00 2001 From: Yossef Mendelssohn Date: Sun, 29 Jun 2008 22:07:47 -0500 Subject: [PATCH] Adding stream mode. The mode is the first option for the FSEvents::Stream object itself (and not the OSX FSEvent stream). It will primarily affect the strategy for getting an event's modified files. --- lib/fsevents/stream.rb | 7 ++++++- spec/stream_spec.rb | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/fsevents/stream.rb b/lib/fsevents/stream.rb index 6a83438..089bdb6 100644 --- a/lib/fsevents/stream.rb +++ b/lib/fsevents/stream.rb @@ -2,11 +2,13 @@ module FSEvents class Stream - attr_reader :stream, :last_event + attr_reader :stream, :mode, :last_event attr_reader :allocator, :context, :paths, :since, :latency, :flags, :callback class StreamError < StandardError; end + MODES = [:mtime, :cache] + def initialize(*paths, &callback) raise ArgumentError, 'A callback block is required' if callback.nil? @callback = callback @@ -14,6 +16,9 @@ def initialize(*paths, &callback) options = {} options = paths.pop if paths.last.is_a?(Hash) + @mode = options[:mode] || :mtime + raise ArgumentError, "Mode '#{mode}' unknown" unless MODES.include?(@mode) + paths = Dir.pwd if paths.empty? @allocator = options[:allocator] || OSX::KCFAllocatorDefault diff --git a/spec/stream_spec.rb b/spec/stream_spec.rb index 5dd5487..024bb1f 100644 --- a/spec/stream_spec.rb +++ b/spec/stream_spec.rb @@ -50,6 +50,7 @@ [:allocator, :context, :since, :latency, :flags].each do |opt| @options[opt] = stub(opt.to_s) end + @options[:mode] = :cache @other_path = '/other/path' end @@ -113,6 +114,19 @@ @options.delete(:flags) FSEvents::Stream.new(@path, @options) {}.flags.should == 0 end + + it 'should store mode' do + FSEvents::Stream.new(@path, @options) {}.mode.should == @options[:mode] + end + + it 'should default mode to mtime' do + @options.delete(:mode) + FSEvents::Stream.new(@path, @options) {}.mode.should == :mtime + end + + it 'should not accept any mode other than mtime or cache' do + lambda { FSEvents::Stream.new(@path, @options.merge(:mode => :something_else)) {} }.should raise_error(ArgumentError) + end end end