Class: FunWith::Files::Watchers::DirectoryWatcher
- Inherits:
-
NodeWatcher
- Object
- NodeWatcher
- FunWith::Files::Watchers::DirectoryWatcher
- Defined in:
- lib/fun_with/files/watchers/directory_watcher.rb
Instance Attribute Summary
Attributes inherited from NodeWatcher
Instance Method Summary collapse
- #all_paths ⇒ Object
- #find_new_files ⇒ Object
-
#initialize(path) ⇒ DirectoryWatcher
constructor
A new instance of DirectoryWatcher.
-
#report_files(paths, status) ⇒ Object
modify the current list of changes by adding “deleted” for every file/folder below this one.
-
#update ⇒ Object
returns a hash of changes.
- #update_existing_files ⇒ Object
Methods inherited from NodeWatcher
#create_watchers, #new_changeset, #set_path
Constructor Details
#initialize(path) ⇒ DirectoryWatcher
Returns a new instance of DirectoryWatcher.
5 6 7 8 |
# File 'lib/fun_with/files/watchers/directory_watcher.rb', line 5 def initialize( path ) set_path( path ) @watchers = create_watchers( self.path.entries ) end |
Instance Method Details
#all_paths ⇒ Object
61 62 63 |
# File 'lib/fun_with/files/watchers/directory_watcher.rb', line 61 def all_paths @watchers.map{|path, watcher| watcher.all_paths }.flatten + [self.path] end |
#find_new_files ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/fun_with/files/watchers/directory_watcher.rb', line 38 def find_new_files # next, get the updated list of files/folders beneath this directory current_paths = self.path.entries for path in current_paths unless @watchers.has_key?( path ) w = Watcher.factory( path ) report_files( w.all_paths, :created ) @watchers[path] = w end end end |
#report_files(paths, status) ⇒ Object
modify the current list of changes by adding “deleted” for every file/folder below this one.
55 56 57 58 59 |
# File 'lib/fun_with/files/watchers/directory_watcher.rb', line 55 def report_files( paths, status ) for path in paths @changes[path] = status end end |
#update ⇒ Object
returns a hash of changes
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fun_with/files/watchers/directory_watcher.rb', line 12 def update new_changeset do if self.path.exist? update_existing_files find_new_files else # If the directory is gone, you can assume the same is true # for all the files it held. report_files( self.all_paths, :deleted ) end end end |
#update_existing_files ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fun_with/files/watchers/directory_watcher.rb', line 25 def update_existing_files # first, check on the files we're supposed to be keeping track of for path, watcher in @watchers @changes[path] = :deleted unless path.exist? @changes.merge!( watcher.update ) # While the main Watcher will continue to monitor the places it's # been told, even if they're missing, the subwatchers just disappear # when the files they're watching do. @watchers.delete( path ) unless path.exist? end end |