Class: Rerun::Watcher
- Inherits:
-
Object
- Object
- Rerun::Watcher
- Defined in:
- lib/watcher.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Directory, FoundFile, InvalidDirectoryError, InvalidFileError
Constant Summary collapse
- CREATED =
0
- MODIFIED =
1
- DELETED =
2
Instance Attribute Summary collapse
-
#directories ⇒ Object
readonly
Returns the value of attribute directories.
-
#priority ⇒ Object
Returns the value of attribute priority.
-
#sleep_time ⇒ Object
Returns the value of attribute sleep_time.
Instance Method Summary collapse
-
#add_directory(dir, expression = "**/*") ⇒ Object
add a directory to be watched.
-
#add_file(file) ⇒ Object
add a specific file to the watch list.
-
#initialize(&client_callback) ⇒ Watcher
constructor
A new instance of Watcher.
-
#join ⇒ Object
wait for the filewatcher to finish.
- #prime ⇒ Object
- #remove_directory(dir) ⇒ Object
- #remove_file(file) ⇒ Object
- #start ⇒ Object
-
#stop ⇒ Object
kill the filewatcher thread.
Constructor Details
#initialize(&client_callback) ⇒ Watcher
Returns a new instance of Watcher.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/watcher.rb', line 18 def initialize(&client_callback) @client_callback = client_callback @sleep_time = 1 @priority = 0 @directories = [] @files = [] @found = nil @first_time = true @thread = nil end |
Instance Attribute Details
#directories ⇒ Object (readonly)
Returns the value of attribute directories.
16 17 18 |
# File 'lib/watcher.rb', line 16 def directories @directories end |
#priority ⇒ Object
Returns the value of attribute priority.
15 16 17 |
# File 'lib/watcher.rb', line 15 def priority @priority end |
#sleep_time ⇒ Object
Returns the value of attribute sleep_time.
15 16 17 |
# File 'lib/watcher.rb', line 15 def sleep_time @sleep_time end |
Instance Method Details
#add_directory(dir, expression = "**/*") ⇒ Object
add a directory to be watched
36 37 38 39 40 41 42 |
# File 'lib/watcher.rb', line 36 def add_directory(dir, expression="**/*") if FileTest.exists?(dir) && FileTest.readable?(dir) then @directories << Directory.new(dir, expression) else raise InvalidDirectoryError, "Dir '#{dir}' either doesnt exist or isnt readable" end end |
#add_file(file) ⇒ Object
add a specific file to the watch list
50 51 52 53 54 55 56 |
# File 'lib/watcher.rb', line 50 def add_file(file) if FileTest.exists?(file) && FileTest.readable?(file) then @files << file else raise InvalidFileError, "File '#{file}' either doesnt exist or isnt readable" end end |
#join ⇒ Object
wait for the filewatcher to finish
104 105 106 107 108 |
# File 'lib/watcher.rb', line 104 def join @thread.join() if @thread rescue Interrupt => e # don't care end |
#prime ⇒ Object
62 63 64 65 66 67 |
# File 'lib/watcher.rb', line 62 def prime @first_time = true @found = Hash.new() examine @first_time = false end |
#remove_directory(dir) ⇒ Object
44 45 46 |
# File 'lib/watcher.rb', line 44 def remove_directory(dir) @directories.delete(dir) end |
#remove_file(file) ⇒ Object
58 59 60 |
# File 'lib/watcher.rb', line 58 def remove_file(file) @files.delete(file) end |
#start ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/watcher.rb', line 69 def start if @thread then raise RuntimeError, "already started" end prime @thread = Thread.new do while true do examine sleep(@sleep_time) end end @thread.priority = @priority at_exit { stop } #? end |
#stop ⇒ Object
kill the filewatcher thread
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/watcher.rb', line 90 def stop begin @thread.wakeup rescue ThreadError => e # ignore end begin @thread.kill rescue ThreadError => e # ignore end end |