Class: Vm::Watcher::VmWatcher
- Inherits:
-
Object
- Object
- Vm::Watcher::VmWatcher
- Includes:
- Observable
- Defined in:
- lib/vm-watcher.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ VmWatcher
constructor
A new instance of VmWatcher.
- #notify(modified) ⇒ Object
- #watch ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ VmWatcher
Returns a new instance of VmWatcher.
29 30 31 32 33 |
# File 'lib/vm-watcher.rb', line 29 def initialize( = {}) @last_mtime = @last_ctime = Time.now @watch = [:watch] @sleep_interval = [:interval] end |
Instance Method Details
#notify(modified) ⇒ Object
55 56 57 58 59 |
# File 'lib/vm-watcher.rb', line 55 def notify(modified) @last_mtime, @last_ctime = modified.mtime, modified.ctime changed notify_observers end |
#watch ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vm-watcher.rb', line 35 def watch files = Dir.glob(@watch).map {|f| Pathname(f). } if files.empty? abort "No files to watch under #{@watch}" end loop do last_modified = files.max {|a, b| a.mtime <=> b.mtime} if last_modified.mtime > @last_mtime notify(last_modified) else last_changed = files.max {|a, b| a.ctime <=> b.ctime} notify(last_changed) if last_changed.ctime > @last_ctime end sleep @sleep_interval end end |