Class: FileWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/filewatcher.rb

Overview

Simple file watcher. Detect changes in files and directories.

Issues: Currently doesn’t monitor changes in directorynames

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unexpanded_filenames, print_filelist = false) ⇒ FileWatcher

Returns a new instance of FileWatcher.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/filewatcher.rb', line 12

def initialize(unexpanded_filenames, print_filelist=false)
  @unexpanded_filenames = unexpanded_filenames
  @last_mtimes = { }
  @filenames = expand_directories(@unexpanded_filenames)

  puts 'Watching:' if print_filelist
  @filenames.each do |filename|
    raise 'File does not exist' unless File.exist?(filename)
    @last_mtimes[filename] = File.stat(filename).mtime
    puts filename if print_filelist
  end
end

Instance Attribute Details

#filenamesObject

Returns the value of attribute filenames.



6
7
8
# File 'lib/filewatcher.rb', line 6

def filenames
  @filenames
end

Class Method Details

.VERSIONObject



8
9
10
# File 'lib/filewatcher.rb', line 8

def self.VERSION
  return '0.3.6'
end

Instance Method Details

#expand_directories(patterns) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/filewatcher.rb', line 79

def expand_directories(patterns)
  if(!patterns.kind_of?Array)
    patterns = [patterns]
  end

  patterns.map { |it| Dir[fulldepth(it)] }.flatten.uniq
end

#filesystem_updated?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/filewatcher.rb', line 36

def filesystem_updated?
  filenames = expand_directories(@unexpanded_filenames)

  if(filenames.size > @filenames.size)
    filename = (filenames - @filenames).first
    @filenames << filename
    @last_mtimes[filename] = File.stat(filename).mtime
    @updated_file = filename
    @event = :new
    return true
  end

  if(filenames.size < @filenames.size)
    filename = (@filenames - filenames).first
    @filenames.delete(filename)
    @last_mtimes.delete(filename)
    @updated_file = filename
    @event = :delete
    return true
  end

  @filenames.each do |filename|
    if(not(File.exists?(filename)))
      @filenames.delete(filename)
      @last_mtimes.delete(filename)
      @updated_file = filename
      @event = :delete
      return true
    end
    mtime = File.stat(filename).mtime
    updated = @last_mtimes[filename] < mtime

    @last_mtimes[filename] = mtime
    if(updated)
      @updated_file = filename
      @event = :changed
      return true
    end
  end

  return false
end

#watch(sleep = 1, &on_update) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/filewatcher.rb', line 25

def watch(sleep=1, &on_update)
  loop do
    begin
      Kernel.sleep sleep until filesystem_updated?
    rescue SystemExit,Interrupt
      Kernel.exit
    end
    yield @updated_file, @event
  end
end