Class: FileMon

Inherits:
Object
  • Object
show all
Defined in:
bin/vrtx-sync

Overview

Monitor files

Instance Method Summary collapse

Constructor Details

#initialize(filenames) ⇒ FileMon

Returns a new instance of FileMon.



37
38
39
40
41
42
43
44
# File 'bin/vrtx-sync', line 37

def initialize(filenames)
  @last_mtimes = { }
  filenames.each do |filename|
    raise "File does not exist: " + filename unless File.exist?(filename)
    @last_mtimes[filename] = File.stat(filename).mtime
  end
  @filenames = filenames
end

Instance Method Details

#file_updated?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
# File 'bin/vrtx-sync', line 53

def file_updated?
  @filenames.each do |filename|
    mtime = File.stat(filename).mtime
    updated = @last_mtimes[filename] < mtime
    @last_mtimes[filename] = mtime
    if(updated)
      @updated_file = filename
      return true
    end
  end
  return false
end

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



46
47
48
49
50
51
# File 'bin/vrtx-sync', line 46

def run(sleep=1, &on_update)
  loop do
    Kernel.sleep sleep until file_updated?
    yield @updated_file
  end
end