Class: Rerun::Watcher

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

Direct Known Subclasses

FSWatcher, OSXWatcher

Defined Under Namespace

Classes: Directory, FoundFile, InvalidDirectoryError, InvalidFileError

Constant Summary collapse

CREATED =
0
MODIFIED =
1
DELETED =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

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/rerun/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

#directoriesObject (readonly)

Returns the value of attribute directories.



16
17
18
# File 'lib/rerun/watcher.rb', line 16

def directories
  @directories
end

#priorityObject

Returns the value of attribute priority.



15
16
17
# File 'lib/rerun/watcher.rb', line 15

def priority
  @priority
end

#sleep_timeObject

Returns the value of attribute sleep_time.



15
16
17
# File 'lib/rerun/watcher.rb', line 15

def sleep_time
  @sleep_time
end

Instance Method Details

#add_directory(dir, expression = "**/*") ⇒ Object

add a directory to be watched

Parameters:

  • dir

    the directory to watch

  • expression (defaults to: "**/*")

    the glob pattern to search under the watched directory



36
37
38
39
40
41
42
# File 'lib/rerun/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

Parameters:

  • file

    the file to watch



50
51
52
53
54
55
56
# File 'lib/rerun/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

#joinObject

wait for the filewatcher to finish



104
105
106
107
108
# File 'lib/rerun/watcher.rb', line 104

def join
  @thread.join() if @thread
rescue Interrupt => e
  # don't care
end

#primeObject



62
63
64
65
66
67
# File 'lib/rerun/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/rerun/watcher.rb', line 44

def remove_directory(dir)
  @directories.delete(dir)
end

#remove_file(file) ⇒ Object



58
59
60
# File 'lib/rerun/watcher.rb', line 58

def remove_file(file)
  @files.delete(file)
end

#startObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rerun/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

#stopObject

kill the filewatcher thread



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rerun/watcher.rb', line 90

def stop
  begin
    @thread.wakeup
  rescue ThreadError => e
    # ignore
  end
  begin
    @thread.kill
  rescue ThreadError => e
    # ignore
  end
end