class Gazr::EventHandler::Darwin

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/gazr/event_handlers/darwin.rb, line 13
def initialize
  super
  self.latency = 0.2
end

Public Instance Methods

listen(monitored_paths) click to toggle source
# File lib/gazr/event_handlers/darwin.rb, line 18
def listen(monitored_paths)
  register_paths(monitored_paths)
  start
end
refresh(monitored_paths) click to toggle source
# File lib/gazr/event_handlers/darwin.rb, line 23
def refresh(monitored_paths)
  register_paths(monitored_paths)
  restart
end

Private Instance Methods

detect_change(dir) click to toggle source
# File lib/gazr/event_handlers/darwin.rb, line 37
def detect_change(dir)
  paths = monitored_paths_for(dir)
  type  = nil
  path  = paths.find {|path| type = event_type(path) }

  FSEvents.debug("event detection error") if type.nil?

  update_reference_times
  [path, type]
end
dirs_for(paths) click to toggle source
# File lib/gazr/event_handlers/darwin.rb, line 67
def dirs_for(paths)
  paths.map {|path| path.dirname.to_s }.uniq
end
event_type(path) click to toggle source
# File lib/gazr/event_handlers/darwin.rb, line 48
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
monitored_paths_for(dir) click to toggle source
# File lib/gazr/event_handlers/darwin.rb, line 56
def monitored_paths_for(dir)
  dir = Pathname(dir).expand_path
  @paths.select {|path| path.dirname.expand_path == dir }
end
on_change(dirs) click to toggle source
# File lib/gazr/event_handlers/darwin.rb, line 30
def on_change(dirs)
  dirs.each do |dir|
    path, type = detect_change(dir)
    notify(path, type) unless path.nil?
  end
end
register_paths(paths) click to toggle source
# File lib/gazr/event_handlers/darwin.rb, line 61
def register_paths(paths)
  @paths = paths
  watch_directories(dirs_for(@paths))
  update_reference_times
end
update_reference_times() click to toggle source
# File lib/gazr/event_handlers/darwin.rb, line 71
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