Class: FireflyServer::FileWatcher
- Inherits:
-
Object
- Object
- FireflyServer::FileWatcher
- Defined in:
- lib/firefly_server/file_watcher.rb
Defined Under Namespace
Classes: ChangeEvent
Instance Method Summary collapse
-
#initialize(configuration) ⇒ FileWatcher
constructor
A new instance of FileWatcher.
- #watch!(&on_change_callback) ⇒ Object
Constructor Details
#initialize(configuration) ⇒ FileWatcher
Returns a new instance of FileWatcher.
7 8 9 10 |
# File 'lib/firefly_server/file_watcher.rb', line 7 def initialize(configuration) @configuration = configuration @listener = nil end |
Instance Method Details
#watch!(&on_change_callback) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/firefly_server/file_watcher.rb', line 12 def watch!(&on_change_callback) # prevent multiple listeners listener.stop if listener # always mute the Listen logger due to verbosity if !Listen.logger Listen.logger = Logger.new(STDOUT) Listen.logger.level = Logger::WARN end @listener = Listen.to(*configuration.watch_paths) do |modified, added, removed| ignored = [] [modified, added, removed].each do |paths| # remove ignored paths paths.reject! do |path| configuration.ignore_paths.any? do |ignore_path| is_ignored = case ignore_path when String then path.start_with?(ignore_path) when Regexp then path =~ ignore_path else raise( ArgumentError, "unknown ignore path (expected string or regex): #{ignore_path.class} - #{ignore_path.inspect}" ) end ignored << path if is_ignored end end end # trigger change callbacks callbacks = (configuration.on_change_callbacks + [on_change_callback]).compact if !callbacks.empty? change_event = ChangeEvent.new( ignored: ignored, modified: modified, added: added, removed: removed ) callbacks.each do |callback| callback.call(change_event) end end end listener.start self end |