/
darwin.rb
70 lines (59 loc) · 1.73 KB
/
darwin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module Watchr
module EventHandler
class Darwin < FSEvent
include Base
def initialize
super
self.latency = 0.2
end
def listen(monitored_paths)
register_paths(monitored_paths)
start
end
def refresh(monitored_paths)
register_paths(monitored_paths)
restart
end
private
def on_change(dirs)
dirs.each {|dir| notify(*detect_change_in(dir)) }
end
def detect_change_in(dir)
paths = monitored_paths_for(dir)
paths.each do |path|
type = event_type(path)
return [path, type] if type
end
end
def event_type(path)
return :deleted if !path.exist?
return :modified if path.mtime > @reference_times[path][:mtime]
return :accessed if path.atime > @reference_times[path][:atime]
return :changed if path.ctime > @reference_times[path][:ctime]
nil
end
def monitored_paths_for(dir)
dir = Pathname(dir).expand_path
@paths.select {|path| path.dirname.expand_path == dir }
end
def register_paths(paths)
@paths = paths
watch_directories(dirs_for(@paths))
update_reference_times
end
def dirs_for(paths)
paths.map {|path| path.dirname.to_s }.uniq
end
def update_reference_times
@reference_times = {}
now = Time.now
@paths.each do |path|
@reference_times[path] = {}
@reference_times[path][:atime] = now
@reference_times[path][:mtime] = now
@reference_times[path][:ctime] = now
end
end
end
end
end