lib/watchr/event_handlers/unix.rb in watchr-0.5.6 vs lib/watchr/event_handlers/unix.rb in watchr-0.5.7
- old
+ new
@@ -10,13 +10,50 @@
# Stores a reference back to handler so we can call its #nofity
# method with file event info
attr_accessor :handler
end
+ def initialize(path)
+ super
+ update_reference_times
+ end
+
+ # File's path as a Pathname
+ def pathname
+ @pathname ||= Pathname(@path)
+ end
+
# Callback. Called on file change event
# Delegates to Controller#update, passing in path and event type
def on_change
- self.class.handler.notify(path, :changed)
+ self.class.handler.notify(path, type)
+ update_reference_times unless type == :deleted
+ end
+
+ private
+
+ def update_reference_times
+ @reference_atime = pathname.atime
+ @reference_mtime = pathname.mtime
+ @reference_ctime = pathname.ctime
+ end
+
+ # Type of latest event.
+ #
+ # A single type is determined, even though more than one stat times may
+ # have changed on the file. The type is the first to match in the
+ # following hierarchy:
+ #
+ # :deleted, :modified (mtime), :accessed (atime), :changed (ctime)
+ #
+ # ===== Returns
+ # type<Symbol>:: latest event's type
+ #
+ def type
+ return :deleted if !pathname.exist?
+ return :modified if pathname.mtime > @reference_mtime
+ return :accessed if pathname.atime > @reference_atime
+ return :changed if pathname.ctime > @reference_ctime
end
end
def initialize
SingleFileWatcher.handler = self