Class: Ramaze::Reloader::WatchStat
- Inherits:
-
Object
- Object
- Ramaze::Reloader::WatchStat
- Defined in:
- lib/ramaze/reloader/watch_stat.rb
Instance Method Summary collapse
- #call(cooldown) ⇒ Object
-
#changed_files ⇒ Object
return files changed since last call.
-
#close ⇒ Object
no need for cleanup.
-
#initialize ⇒ WatchStat
constructor
A new instance of WatchStat.
-
#remove_watch(file) ⇒ Object
stop watching a file for changes.
- #safe_stat(file) ⇒ Object
-
#watch(file) ⇒ Object
start watching a file for changes true if succeeded, false if failure.
- #watching?(file) ⇒ Boolean
Constructor Details
#initialize ⇒ WatchStat
Returns a new instance of WatchStat.
4 5 6 7 8 |
# File 'lib/ramaze/reloader/watch_stat.rb', line 4 def initialize # @files[file_path] = stat @files = {} @last = Time.now end |
Instance Method Details
#call(cooldown) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/ramaze/reloader/watch_stat.rb', line 10 def call(cooldown) if cooldown and Time.now > @last + cooldown yield @last = Time.now end end |
#changed_files ⇒ Object
return files changed since last call
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ramaze/reloader/watch_stat.rb', line 40 def changed_files @files.each do |file, stat| if new_stat = safe_stat(file) if new_stat.mtime > stat.mtime @files[file] = new_stat yield(file) end end end end |
#close ⇒ Object
no need for cleanup
36 37 |
# File 'lib/ramaze/reloader/watch_stat.rb', line 36 def close end |
#remove_watch(file) ⇒ Object
stop watching a file for changes
31 32 33 |
# File 'lib/ramaze/reloader/watch_stat.rb', line 31 def remove_watch(file) @files.delete(file) end |
#safe_stat(file) ⇒ Object
51 52 53 54 55 |
# File 'lib/ramaze/reloader/watch_stat.rb', line 51 def safe_stat(file) File.stat(file) rescue Errno::ENOENT, Errno::ENOTDIR nil end |
#watch(file) ⇒ Object
start watching a file for changes true if succeeded, false if failure
19 20 21 22 23 24 |
# File 'lib/ramaze/reloader/watch_stat.rb', line 19 def watch(file) return true if watching?(file) # if already watching if stat = safe_stat(file) @files[file] = stat end end |
#watching?(file) ⇒ Boolean
26 27 28 |
# File 'lib/ramaze/reloader/watch_stat.rb', line 26 def watching?(file) @files.has_key?(file) end |