directory_watcher 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/directory_watcher.rb +33 -34
  2. metadata +2 -2
@@ -164,8 +164,6 @@ class DirectoryWatcher
164
164
  #
165
165
  Event = Struct.new :type, :path
166
166
 
167
- FileInfo = Struct.new :mtime, :size, :stable # :nodoc:
168
-
169
167
  #
170
168
  # call-seq:
171
169
  # DirectoryWatcher.new( directory, options )
@@ -206,6 +204,7 @@ class DirectoryWatcher
206
204
 
207
205
  @files = (opts[:pre_load] ? scan_files : Hash.new)
208
206
  @events = []
207
+ @thread = nil
209
208
  end
210
209
 
211
210
  #
@@ -371,17 +370,17 @@ class DirectoryWatcher
371
370
  # scan_files
372
371
  #
373
372
  # Using the configured glob pattern, scan the directory for all files and
374
- # return a hash with the filenames as keys and +FileInfo+ objects as the
375
- # values. The +FileInfo+ objects contain the mtime and size of the file.
373
+ # return a hash with the filenames as keys and +File::Stat+ objects as the
374
+ # values. The +File::Stat+ objects contain the mtime and size of the file.
376
375
  #
377
376
  def scan_files
378
377
  files = {}
379
378
  @glob.each do |glob|
380
379
  Dir.glob(glob).each do |fn|
381
380
  begin
382
- if Kernel.test(?f, fn)
383
- files[fn] = FileInfo.new(File.mtime(fn), File.size(fn))
384
- end
381
+ stat = File.stat fn
382
+ next unless stat.file?
383
+ files[fn] = stat
385
384
  rescue SystemCallError; end
386
385
  end
387
386
  end
@@ -403,15 +402,14 @@ class DirectoryWatcher
403
402
  start = Time.now.to_f
404
403
 
405
404
  files = scan_files
406
- keys = [files.keys, @files.keys]
407
- common = keys.first & keys.last
405
+ keys = [files.keys, @files.keys] # current files, previous files
408
406
 
409
407
  find_added(files, *keys)
410
- find_modified(files, common)
408
+ find_modified(files, *keys)
411
409
  find_removed(*keys)
412
410
 
413
411
  notify_observers
414
- @files = files
412
+ @files = files # store the current file list for the next iteration
415
413
 
416
414
  nap_time = @interval - (Time.now.to_f - start)
417
415
  sleep nap_time if nap_time > 0
@@ -424,9 +422,7 @@ class DirectoryWatcher
424
422
  #
425
423
  # Taking the list of current files, _cur_, and the list of files found
426
424
  # previously, _prev_, figure out which files have been added and generate
427
- # a new file added event for each. The events are returned as an array
428
- # from this method. If no files have been added, the returned array is
429
- # empty.
425
+ # a new file added event for each.
430
426
  #
431
427
  def find_added( files, cur, prev )
432
428
  added = cur - prev
@@ -443,9 +439,7 @@ class DirectoryWatcher
443
439
  #
444
440
  # Taking the list of current files, _cur_, and the list of files found
445
441
  # previously, _prev_, figure out which files have been removed and
446
- # generate a new file removed event for each. The events are returned as
447
- # an array from this method. If no files have been removed, the returned
448
- # array is empty.
442
+ # generate a new file removed event for each.
449
443
  #
450
444
  def find_removed( cur, prev )
451
445
  removed = prev - cur
@@ -455,33 +449,32 @@ class DirectoryWatcher
455
449
 
456
450
  #
457
451
  # call-seq:
458
- # find_modified( files, common )
452
+ # find_modified( files, cur, prev )
459
453
  #
460
- # Taking the list of _common_ files (those that exist in the current file
461
- # list and in the previous file list) determine if any have been modified.
462
- # Generate a new file modified event for each modified file. Also, by
463
- # looking at the stable count in the _files_ hash, figure out if any files
464
- # have become stable since being added or modified. Generate a new stable
465
- # event for each stabilized file. The events are returned as an array from
466
- # this method. If there are no events, the array is empty.
454
+ # Taking the list of current files, _cur_, and the list of files found
455
+ # previously, _prev_, find those that are common between them and determine
456
+ # if any have been modified. Generate a new file modified event for each
457
+ # modified file. Also, by looking at the stable count in the _files_ hash,
458
+ # figure out if any files have become stable since being added or modified.
459
+ # Generate a new stable event for each stabilized file.
467
460
  #
468
- def find_modified( files, common )
469
- common.each do |key|
470
- cur, prev = files[key], @files[key]
461
+ def find_modified( files, cur, prev )
462
+ (cur & prev).each do |key|
463
+ cur_stat, prev_stat = files[key], @files[key]
471
464
 
472
465
  # if the modification time or the file size differs from the last
473
466
  # time it was seen, then create a :modified event
474
- if cur.mtime != prev.mtime or cur.size != prev.size
467
+ if (cur_stat <=> prev_stat) != 0 or cur_stat.size != prev_stat.size
475
468
  @events << Event.new(:modified, key)
476
- cur.stable = @stable
469
+ cur_stat.stable = @stable
477
470
 
478
471
  # otherwise, if the count is not nil see if we need to create a
479
472
  # :stable event
480
- elsif !prev.stable.nil?
481
- cur.stable = prev.stable - 1
482
- if cur.stable == 0
473
+ elsif !prev_stat.stable.nil?
474
+ cur_stat.stable = prev_stat.stable - 1
475
+ if cur_stat.stable == 0
483
476
  @events << Event.new(:stable, key)
484
- cur.stable = nil
477
+ cur_stat.stable = nil
485
478
  end
486
479
  end
487
480
  end
@@ -507,4 +500,10 @@ class DirectoryWatcher
507
500
 
508
501
  end # class DirectoryWatcher
509
502
 
503
+ # :stopdoc:
504
+ # We need to add a 'stable' attribute to the File::Stat object
505
+ class File::Stat
506
+ attr_accessor :stable
507
+ end
508
+
510
509
  # EOF
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: directory_watcher
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2006-11-10 00:00:00 -07:00
6
+ version: 0.1.2
7
+ date: 2006-11-26 00:00:00 -07:00
8
8
  summary: A class for watching files within a directory and generating events when those files change
9
9
  require_paths:
10
10
  - lib