Class: Filewatcher
- Inherits:
-
Object
- Object
- Filewatcher
- Includes:
- Cycles
- Defined in:
- lib/confinement/filewatcher/filewatcher.rb,
lib/confinement/filewatcher/filewatcher/cycles.rb,
lib/confinement/filewatcher/filewatcher/version.rb
Overview
Simple file watcher. Detect changes in files and directories.
Issues: Currently doesn’t monitor changes in directorynames
Defined Under Namespace
Modules: Cycles
Constant Summary collapse
- VERSION =
'1.1.1'.freeze
Instance Attribute Summary collapse
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#keep_watching ⇒ Object
readonly
Returns the value of attribute keep_watching.
Instance Method Summary collapse
-
#finalize(&on_update) ⇒ Object
Calls the update block repeatedly until all changes in the current snapshot are dealt with.
-
#initialize(unexpanded_filenames, options = {}) ⇒ Filewatcher
constructor
A new instance of Filewatcher.
- #last_found_filenames ⇒ Object
- #pause ⇒ Object
- #resume ⇒ Object
-
#stop ⇒ Object
Ends the watch, allowing any remaining changes to be finalized.
- #update_spinner(label) ⇒ Object
- #watch(&on_update) ⇒ Object
Constructor Details
#initialize(unexpanded_filenames, options = {}) ⇒ Filewatcher
Returns a new instance of Filewatcher.
20 21 22 23 24 25 26 27 28 |
# File 'lib/confinement/filewatcher/filewatcher.rb', line 20 def initialize(, = {}) @unexpanded_filenames = @unexpanded_excluded_filenames = [:exclude] @keep_watching = false @pausing = false @immediate = [:immediate] @show_spinner = [:spinner] @interval = .fetch(:interval, 0.5) end |
Instance Attribute Details
#interval ⇒ Object
Returns the value of attribute interval.
11 12 13 |
# File 'lib/confinement/filewatcher/filewatcher.rb', line 11 def interval @interval end |
#keep_watching ⇒ Object (readonly)
Returns the value of attribute keep_watching.
12 13 14 |
# File 'lib/confinement/filewatcher/filewatcher.rb', line 12 def keep_watching @keep_watching end |
Instance Method Details
#finalize(&on_update) ⇒ Object
Calls the update block repeatedly until all changes in the current snapshot are dealt with
70 71 72 73 74 75 76 77 |
# File 'lib/confinement/filewatcher/filewatcher.rb', line 70 def finalize(&on_update) on_update = @on_update unless block_given? while filesystem_updated?(@end_snapshot || mtime_snapshot) update_spinner('Finalizing') trigger_changes(on_update) end @end_snapshot = nil end |
#last_found_filenames ⇒ Object
79 80 81 |
# File 'lib/confinement/filewatcher/filewatcher.rb', line 79 def last_found_filenames last_snapshot.keys end |
#pause ⇒ Object
43 44 45 46 47 48 |
# File 'lib/confinement/filewatcher/filewatcher.rb', line 43 def pause @pausing = true update_spinner('Initiating pause') # Ensure we wait long enough to enter pause loop in #watch sleep @interval end |
#resume ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/confinement/filewatcher/filewatcher.rb', line 50 def resume if !@keep_watching || !@pausing raise "Can't resume unless #watch and #pause were first called" end @last_snapshot = mtime_snapshot # resume with fresh snapshot @pausing = false update_spinner('Resuming') sleep @interval # Wait long enough to exit pause loop in #watch end |
#stop ⇒ Object
Ends the watch, allowing any remaining changes to be finalized. Used mainly in multi-threaded situations.
62 63 64 65 66 |
# File 'lib/confinement/filewatcher/filewatcher.rb', line 62 def stop @keep_watching = false update_spinner('Stopping') nil end |
#update_spinner(label) ⇒ Object
14 15 16 17 18 |
# File 'lib/confinement/filewatcher/filewatcher.rb', line 14 def update_spinner(label) return unless @show_spinner @spinner ||= %w[\\ | / -] print "#{' ' * 30}\r#{label} #{@spinner.rotate!.first}\r" end |
#watch(&on_update) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/confinement/filewatcher/filewatcher.rb', line 30 def watch(&on_update) @on_update = on_update @keep_watching = true yield('', '') if @immediate main_cycle @end_snapshot = mtime_snapshot finalize(&on_update) ensure stop end |