Class: OsxWatchfolder::FolderWatcher

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

Overview

To run the method ‘run_tests’ when a change is detected in a couple of folders:

folder_watcher = OsxWatchfolder::FolderWatcher.new("lib", "test") { run_tests}
folder_wacher.start

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*folders, &block) ⇒ FolderWatcher

Returns a new instance of FolderWatcher.



41
42
43
44
45
46
47
48
# File 'lib/osx_watchfolder/folder_watcher.rb', line 41

def initialize(*folders, &block)
  @folders = Array === folders.first ? folders.first : folders
  @block = block
  @running = true
  @latency = 1
  @runloop_interval = 5
  @interrupted_count = 0
end

Instance Attribute Details

#interrupted_countObject (readonly)

how many times has this been interrupted?



21
22
23
# File 'lib/osx_watchfolder/folder_watcher.rb', line 21

def interrupted_count
  @interrupted_count
end

#latencyObject

directory update notification latency in seconds. Defaults to 1



14
15
16
# File 'lib/osx_watchfolder/folder_watcher.rb', line 14

def latency
  @latency
end

#runloop_intervalObject

runloop timeout in seconds - on all but the 1st interrupt for the script it will take a maximum of this long for interrupts to be noticed. Defaults to 5



18
19
20
# File 'lib/osx_watchfolder/folder_watcher.rb', line 18

def runloop_interval
  @runloop_interval
end

Instance Method Details

#startObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/osx_watchfolder/folder_watcher.rb', line 29

def start
  raise "May only be started from main thread" unless Thread.current == Thread.main
  prepare_stream
  enter_run_loop
rescue Interrupt
  stop
  @interrupted_count += 1
ensure
  cleanup_stream 
end

#stopObject



24
25
26
27
# File 'lib/osx_watchfolder/folder_watcher.rb', line 24

def stop
  @running = false
  OSX::CFRunLoopStop(@runloop)
end