Class: DirWatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(dir, options = {}, &block) ⇒ DirWatcher

Returns a new instance of DirWatcher.



4
5
6
7
8
9
# File 'lib/dir_watcher.rb', line 4

def initialize(dir, options = {}, &block)
  @dir = dir
  @block = block
  @sleep_time = options[:sleep_time] || 1
  run
end

Instance Method Details

#on_changeObject



23
24
25
# File 'lib/dir_watcher.rb', line 23

def on_change
  @block.call
end

#runObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/dir_watcher.rb', line 11

def run
  while true
    new_run = `ls -lahRT #{@dir}`
    if new_run != @last_run
      on_change
    end
    @last_run = new_run
    sleep @sleep_time
  end
end