class Filewatcher

Simple file watcher. Detect changes in files and directories.

Issues: Currently doesn’t monitor changes in directory names

Helpers in Filewatcher class itself

Constants

VERSION

Attributes

interval[RW]
keep_watching[R]

Public Class Methods

new(unexpanded_filenames, options = {}) click to toggle source
# File lib/filewatcher.rb, line 26
def initialize(unexpanded_filenames, options = {})
  @unexpanded_filenames = unexpanded_filenames
  @unexpanded_excluded_filenames = options[:exclude]
  @keep_watching = false
  @pausing = false
  @immediate = options[:immediate]
  @interval = options.fetch(:interval, 0.5)
  @logger = options.fetch(:logger, Logger.new($stdout, level: :info))

  after_initialize unexpanded_filenames, options
end
print_version() click to toggle source
Calls superclass method

Public Instance Methods

finalize(&on_update) click to toggle source

Calls the update block repeatedly until all changes in the current snapshot are dealt with

# File lib/filewatcher.rb, line 87
def finalize(&on_update)
  on_update = @on_update unless block_given?

  while file_system_updated?(@end_snapshot || current_snapshot)
    finalizing
    trigger_changes(on_update)
  end

  @end_snapshot = nil
end
pause() click to toggle source
# File lib/filewatcher.rb, line 55
def pause
  @pausing = true

  before_pause_sleep

  # Ensure we wait long enough to enter pause loop in #watch
  sleep @interval
end
resume() click to toggle source
# File lib/filewatcher.rb, line 64
def resume
  raise "Can't resume unless #watch and #pause were first called" if !@keep_watching || !@pausing

  @last_snapshot = current_snapshot # resume with fresh snapshot
  @pausing = false

  before_resume_sleep

  sleep @interval # Wait long enough to exit pause loop in #watch
end
stop() click to toggle source

Ends the watch, allowing any remaining changes to be finalized. Used mainly in multi-threaded situations.

# File lib/filewatcher.rb, line 77
def stop
  @keep_watching = false

  after_stop

  nil
end
watch() { |{ '' => '' }| ... } click to toggle source
# File lib/filewatcher.rb, line 38
def watch(&on_update)
  ## The set of available signals depends on the OS
  ## Windows doesn't support `HUP` signal, for example
  (%w[HUP INT TERM] & Signal.list.keys).each do |signal|
    trap(signal) { exit }
  end

  @on_update = on_update
  @keep_watching = true
  yield({ '' => '' }) if @immediate

  main_cycle

  @end_snapshot = current_snapshot
  finalize(&on_update)
end

Private Instance Methods

after_initialize(*) click to toggle source
Calls superclass method
# File lib/filewatcher.rb, line 104
def after_initialize(*)
  super if defined?(super)
end
after_stop() click to toggle source
Calls superclass method
# File lib/filewatcher.rb, line 116
def after_stop
  super if defined?(super)
end
before_pause_sleep() click to toggle source
Calls superclass method
# File lib/filewatcher.rb, line 108
def before_pause_sleep
  super if defined?(super)
end
before_resume_sleep() click to toggle source
Calls superclass method
# File lib/filewatcher.rb, line 112
def before_resume_sleep
  super if defined?(super)
end
debug(data) click to toggle source
# File lib/filewatcher.rb, line 100
def debug(data)
  @logger.debug "Thread ##{Thread.current.object_id} #{data}"
end
finalizing() click to toggle source
Calls superclass method
# File lib/filewatcher.rb, line 120
def finalizing
  super if defined?(super)
end