Class: FileWatcher
- Inherits:
-
Object
- Object
- FileWatcher
- Defined in:
- lib/file_watcher.rb
Instance Method Summary collapse
-
#create! ⇒ Object
Creates file if not exists.
- #each_change(&block) ⇒ Object
-
#initialize(file) ⇒ FileWatcher
constructor
A new instance of FileWatcher.
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 |