Class: FileWatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ FileWatcher

Returns a new instance of FileWatcher.



2
3
4
5
# File 'lib/file_watcher.rb', line 2

def initialize(file)
  @file = file
  create!
end

Instance Method Details

#create!Object

Creates file if not exists



8
9
10
11
12
13
# File 'lib/file_watcher.rb', line 8

def create!
  if(!File.exist?(@file))
    FileUtils.mkdir_p(@file)
    FileUtils.touch(@file)
  end
end

#each_change(&block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/file_watcher.rb', line 15

def each_change(&block)
  last_stat = nil
  loop do
    out = `stat #{@file}`
    if out != last_stat
      last_stat = out
      yield block
    end
    sleep 0.5
  end
  
end