From 88111595561205e35e5b94a8e0d0963151e90753 Mon Sep 17 00:00:00 2001 From: Yossef Mendelssohn Date: Tue, 17 Jun 2008 15:28:32 -0500 Subject: [PATCH] Extending the events array with #files and #modified_files. --- lib/fsevents.rb | 1 + lib/fsevents/ext.rb | 1 + lib/fsevents/ext/array.rb | 9 +++++++++ lib/fsevents/stream.rb | 1 + spec/event_array_spec.rb | 42 +++++++++++++++++++++++++++++++++++++++ spec/stream_spec.rb | 6 ++++++ 6 files changed, 60 insertions(+) create mode 100644 lib/fsevents/ext.rb create mode 100644 lib/fsevents/ext/array.rb create mode 100644 spec/event_array_spec.rb diff --git a/lib/fsevents.rb b/lib/fsevents.rb index b64fb42..4254c20 100644 --- a/lib/fsevents.rb +++ b/lib/fsevents.rb @@ -4,6 +4,7 @@ require 'osx/foundation' OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework' +require 'fsevents/ext' require 'fsevents/stream' module FSEvents diff --git a/lib/fsevents/ext.rb b/lib/fsevents/ext.rb new file mode 100644 index 0000000..ddb6462 --- /dev/null +++ b/lib/fsevents/ext.rb @@ -0,0 +1 @@ +require 'fsevents/ext/array' diff --git a/lib/fsevents/ext/array.rb b/lib/fsevents/ext/array.rb new file mode 100644 index 0000000..9fe83ea --- /dev/null +++ b/lib/fsevents/ext/array.rb @@ -0,0 +1,9 @@ +module EventArray + def files + collect { |x| x.files }.flatten + end + + def modified_files + collect { |x| x.modified_files }.flatten + end +end diff --git a/lib/fsevents/stream.rb b/lib/fsevents/stream.rb index f4176bc..6a83438 100644 --- a/lib/fsevents/stream.rb +++ b/lib/fsevents/stream.rb @@ -34,6 +34,7 @@ def stream_callback paths.regard_as('*') events = [] + events.extend(EventArray) event_count.times { |i| events << Event.new(event_IDs[i], paths[i], self) } callback.call(events) diff --git a/spec/event_array_spec.rb b/spec/event_array_spec.rb new file mode 100644 index 0000000..3332a5b --- /dev/null +++ b/spec/event_array_spec.rb @@ -0,0 +1,42 @@ +require File.dirname(__FILE__) + '/spec_helper.rb' + +describe EventArray do + before :each do + @event_array = [] + @event_array.extend(EventArray) + end + + it 'should return files' do + @event_array.should respond_to(:files) + end + + describe 'returning files' do + it 'should collect files from its events' do + events = Array.new(3) { stub('event', :files => Array.new(3) { stub('file') }) } + files = [] + events.each do |event| + @event_array << event + files += event.files + end + + @event_array.files.should == files + end + end + + it 'should return modified files' do + @event_array.should respond_to(:modified_files) + end + + describe 'returning modified files' do + it 'should collect modified files from its events' do + events = Array.new(3) { stub('event', :modified_files => Array.new(3) { stub('file') }) } + files = [] + events.each do |event| + @event_array << event + files += event.modified_files + end + + @event_array.modified_files.should == files + end + end +end diff --git a/spec/stream_spec.rb b/spec/stream_spec.rb index 88f4da1..7a26c42 100644 --- a/spec/stream_spec.rb +++ b/spec/stream_spec.rb @@ -270,6 +270,12 @@ @proc.call(*@args) end + it 'should extend the event array' do + @args = @args_hash.values_at(*@callback_arg_order) + @callback.expects(:call).with { |events| events.is_a?(EventArray) } + @proc.call(*@args) + end + it "should update the stream's last event" do @stream.expects(:update_last_event) @proc.call(*@args)