Class: Attractor::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/attractor/watcher.rb

Overview

functionality for watching file system changes

Instance Method Summary collapse

Constructor Details

#initialize(file_prefix, ignores, callback) ⇒ Watcher

Returns a new instance of Watcher.



8
9
10
11
12
# File 'lib/attractor/watcher.rb', line 8

def initialize(file_prefix, ignores, callback)
  @file_prefix = file_prefix
  @callback = callback
  @ignores = ignores.split(",").map(&:strip)
end

Instance Method Details

#watchObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/attractor/watcher.rb', line 14

def watch
  @callback.call
  ignore = @ignores + [/^attractor_output/]

  listener = Listen.to(File.absolute_path(@file_prefix), ignore: ignore) do |modified, _added, _removed|
    if modified
      puts "#{modified.map(&:to_s).join(", ")} modified, recalculating..."
      @callback.call
    end
  end
  listener.start
  sleep
end