Skip to content

Commit

Permalink
Listener.new now accepts a hash of options
Browse files Browse the repository at this point in the history
Options are:
  :extensions -- only watch changed files with these extensions
  :relative_paths -- yield changed paths relative to watched directory
  • Loading branch information
mislav committed Jul 4, 2009
1 parent 97b84c7 commit 497034f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
24 changes: 17 additions & 7 deletions lib/rspactor/listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,32 @@
module RSpactor
# based on http://rails.aizatto.com/2007/11/28/taming-the-autotest-beast-with-fsevents/
class Listener
attr_reader :last_check, :callback, :valid_extensions
attr_reader :last_check, :callback, :options, :dirs

def initialize(valid_extensions = nil)
@valid_extensions = valid_extensions
# {:extensions => ['rb', 'haml'], :relative_paths => true}
def initialize(options = {})
@options = options
timestamp_checked

@callback = lambda do |stream, ctx, num_events, paths, marks, event_ids|
changed_files = extract_changed_files_from_paths(split_paths(paths, num_events))
timestamp_checked
changed_files = relativize_path_names(changed_files) if options[:relative_paths]
yield changed_files unless changed_files.empty?
end
end

def relativize_path_names(files)
for file in files
if dir = @dirs.find { |p| file.index(p) == 0 }
file.sub!(dir + '/', '')
end
end
end

def run(directories)
dirs = Array(directories)
stream = OSX::FSEventStreamCreate(OSX::KCFAllocatorDefault, callback, nil, dirs, OSX::KFSEventStreamEventIdSinceNow, 0.5, 0)
@dirs = Array(directories)
stream = OSX::FSEventStreamCreate(OSX::KCFAllocatorDefault, callback, nil, @dirs, OSX::KFSEventStreamEventIdSinceNow, 0.5, 0)
unless stream
$stderr.puts "Failed to create stream"
exit(1)
Expand Down Expand Up @@ -82,7 +92,7 @@ def file_extension(file)
end

def valid_extension?(file)
valid_extensions.nil? or valid_extensions.include?(file_extension(file))
options[:extensions].nil? or options[:extensions].include?(file_extension(file))
end
end
end
2 changes: 1 addition & 1 deletion lib/rspactor/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def start_interactor
def start_listener
@inspector = Inspector.new(dir)

Listener.new(Inspector::EXTENSIONS) do |files|
Listener.new(:extensions => Inspector::EXTENSIONS) do |files|
spec_changed_files(files) unless git_head_changed?
end.run(dir)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe RSpactor::Listener do
before(:all) do
@listener = described_class.new(%w(rb erb haml))
@listener = described_class.new(:extensions => %w(rb erb haml))
end

it "should be timestamped" do
Expand Down

0 comments on commit 497034f

Please sign in to comment.