class Bashly::Watch
File system watcher - an ergonomic wrapper around the Listen gem
Attributes
Public Class Methods
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
Source
# File lib/bashly/watch.rb, line 13 def on_change(&) start(&) wait ensure stop end
Private Instance Methods
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
Source
# File lib/bashly/watch.rb, line 62 def changes(modified, added, removed) { modified:, added:, removed: } end
Source
# File lib/bashly/watch.rb, line 22 def default_options { force_polling: force_polling?, latency: latency, } end
Source
# File lib/bashly/watch.rb, line 29 def force_polling? !Settings.watch_evented end
Source
# File lib/bashly/watch.rb, line 33 def latency value = Settings.watch_latency.to_f value.positive? ? value : 0.1 end
Source
# File lib/bashly/watch.rb, line 38 def start(&block) raise ArgumentError, 'block required' unless block @listener = build_listener(&block) @listener.start end
Source
# File lib/bashly/watch.rb, line 50 def wait sleep rescue ::Interrupt => e raise Bashly::Interrupt, cause: e end