Class: DirectoryWatcher::EmScanner::Watcher

Inherits:
EventMachine::FileWatch
  • Object
show all
Defined in:
lib/directory_watcher/em_scanner.rb

Overview

:stopdoc:

This is our tailored implementation of the EventMachine FileWatch class. It receives notifications of file events and provides a mechanism to translate the EventMachine events into objects to send to the Scanner that it is initialized with.

The Watcher only responds to modified and deleted events.

This class is required by EventableScanner to institute file watching.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scanner) ⇒ Watcher

Initialize the Watcher using with the given scanner Post initialization, EventMachine will set @path



89
90
91
# File 'lib/directory_watcher/em_scanner.rb', line 89

def initialize( scanner )
  @scanner = scanner
end

Class Method Details

.watch(path, scanner) ⇒ Object



82
83
84
# File 'lib/directory_watcher/em_scanner.rb', line 82

def self.watch( path, scanner )
  EventMachine.watch_file path, Watcher, scanner
end

Instance Method Details

#detachObject

Detach the watcher from the event loop.

Required by EventableScanner as part of the shutdown process.



114
115
116
117
118
# File 'lib/directory_watcher/em_scanner.rb', line 114

def detach
  EventMachine.next_tick do
    stop_watching
  end
end

#file_deletedObject Also known as: file_moved

EventMachine callback for when a watched file is deleted. We convert this to a FileStat object for a removed file.



96
97
98
# File 'lib/directory_watcher/em_scanner.rb', line 96

def file_deleted
  @scanner.on_removed(self, ::DirectoryWatcher::FileStat.for_removed_path(@path))
end

#file_modifiedObject

EventMachine callback for when a watched file is modified. We convert this to a FileStat object and send it to the collector



105
106
107
108
# File 'lib/directory_watcher/em_scanner.rb', line 105

def file_modified
  stat = File.stat @path
  @scanner.on_modified(self, ::DirectoryWatcher::FileStat.new(@path, stat.mtime, stat.size))
end