Class: FileWatcher
- Inherits:
-
Object
- Object
- FileWatcher
- Defined in:
- lib/file_watcher.rb
Instance Attribute Summary collapse
-
#changed ⇒ Object
readonly
Returns the value of attribute changed.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#snapshot ⇒ Object
readonly
Returns the value of attribute snapshot.
Instance Method Summary collapse
-
#files_changed? ⇒ Boolean
have the given files changed if they have then set the changed file and return true.
-
#initialize(files = nil, options = {}) ⇒ FileWatcher
constructor
A new instance of FileWatcher.
- #set_files(files) ⇒ Object
-
#snapshot_filesystem ⇒ Object
take a snapshot of the given files so we can compare at a given date.
-
#watch(have_changed, waiting = nil) ⇒ Object
watching the files given in initialize if the files change call the proc.
Constructor Details
#initialize(files = nil, options = {}) ⇒ FileWatcher
Returns a new instance of FileWatcher.
4 5 6 7 8 9 10 11 12 |
# File 'lib/file_watcher.rb', line 4 def initialize(files = nil, = {}) @files = files || ['./*'] @changed = nil @event = nil if not @files.empty? @snapshot = snapshot_filesystem end @options = end |
Instance Attribute Details
#changed ⇒ Object (readonly)
Returns the value of attribute changed.
2 3 4 |
# File 'lib/file_watcher.rb', line 2 def changed @changed end |
#event ⇒ Object (readonly)
Returns the value of attribute event.
2 3 4 |
# File 'lib/file_watcher.rb', line 2 def event @event end |
#snapshot ⇒ Object (readonly)
Returns the value of attribute snapshot.
2 3 4 |
# File 'lib/file_watcher.rb', line 2 def snapshot @snapshot end |
Instance Method Details
#files_changed? ⇒ Boolean
have the given files changed if they have then set the changed file and return true
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 |
# File 'lib/file_watcher.rb', line 36 def files_changed? # initialise variables for the new_snapshot = snapshot_filesystem has_changed = false # take a new snapshot and subtract this from the old snapshot in order to get forward changes # then add the snapshot to the oposite subtraction to get backward changes changes = (@snapshot.to_a - new_snapshot.to_a) + (new_snapshot.to_a - @snapshot.to_a) # determine the event for each change changes.each do |change| if @snapshot.keys.include? change[0] @changed = {change[0] => change[1]} @event = (new_snapshot.keys.include? change[0]) ? :change : :delete has_changed = true else @changed = {change[0] => change[1]} @event = :new has_changed = true end end # lets reset the snapshot so that we don't create a forever loop @snapshot = new_snapshot return has_changed end |
#set_files(files) ⇒ Object
14 15 16 17 |
# File 'lib/file_watcher.rb', line 14 def set_files(files) @files = files @snapshot = snapshot_filesystem end |
#snapshot_filesystem ⇒ Object
take a snapshot of the given files so we can compare at a given date
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/file_watcher.rb', line 20 def snapshot_filesystem mtimes = {} files = @files.map { |file| Dir[file] }.flatten.uniq files.each do |file| if File.exists? file mtimes[file] = File.stat(file).mtime end end mtimes end |
#watch(have_changed, waiting = nil) ⇒ Object
watching the files given in initialize if the files change call the proc
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/file_watcher.rb', line 65 def watch(have_changed, waiting = nil) @watching = true while @watching if files_changed? @watching = have_changed.call(@changed, @event) end sleep(1) waiting.call if waiting end end |