class Bashly::Watch

File system watcher - an ergonomic wrapper around the Listen gem

Attributes

dirs[R]
options[R]

Public Class Methods

new(*dirs, **options) click to toggle source
# File lib/bashly/watch.rb, line 8
def initialize(*dirs, **options)
  @options = default_options.merge(options).freeze
  @dirs = dirs.empty? ? ['.'] : dirs
end

Public Instance Methods

on_change(&) click to toggle source
# File lib/bashly/watch.rb, line 13
def on_change(&)
  start(&)
  wait
ensure
  stop
end

Private Instance Methods

build_listener() { |changes(modified, added, removed)| ... } click to toggle source
# File lib/bashly/watch.rb, line 56
def build_listener
  listen.to(*dirs, **options) do |modified, added, removed|
    yield changes(modified, added, removed)
  end
end
changes(modified, added, removed) click to toggle source
# File lib/bashly/watch.rb, line 62
def changes(modified, added, removed)
  { modified:, added:, removed: }
end
default_options() click to toggle source
# File lib/bashly/watch.rb, line 22
def default_options
  {
    force_polling: force_polling?,
    latency:       latency,
  }
end
force_polling?() click to toggle source
# File lib/bashly/watch.rb, line 29
def force_polling?
  !Settings.watch_evented
end
latency() click to toggle source
# File lib/bashly/watch.rb, line 33
def latency
  value = Settings.watch_latency.to_f
  value.positive? ? value : 0.1
end
listen(= Listen) click to toggle source
# File lib/bashly/watch.rb, line 66
  def listen = Listen
end
start(&block) click to toggle source
# File lib/bashly/watch.rb, line 38
def start(&block)
  raise ArgumentError, 'block required' unless block

  @listener = build_listener(&block)
  @listener.start
end
stop() click to toggle source
# File lib/bashly/watch.rb, line 45
def stop
  @listener&.stop
  @listener = nil
end
wait() click to toggle source
# File lib/bashly/watch.rb, line 50
def wait
  sleep
rescue ::Interrupt => e
  raise Bashly::Interrupt, cause: e
end