Class: LivereloadRails::Watcher

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

Instance Method Summary collapse

Constructor Details

#initialize(paths, matchers:, &update) ⇒ Watcher

Returns a new instance of Watcher.



3
4
5
6
7
8
# File 'lib/livereload_rails/watcher.rb', line 3

def initialize(paths, matchers:, &update)
  LivereloadRails.logger.debug "Watching #{paths} for changes."
  @watcher = FileWatcher.new(paths)
  @matchers = matchers
  @update = update
end

Instance Method Details

#run(timeout = 0.2) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/livereload_rails/watcher.rb', line 10

def run(timeout = 0.2)
  @watcher.watch(timeout) do |path, event|
    unless FileTest.file?(path)
      LivereloadRails.logger.debug "#{path} -> not a file."
      next
    end

    unless filename = translate(path)
      LivereloadRails.logger.debug "#{path} -> no match."
      next
    end

    if filename.empty?
      LivereloadRails.logger.debug "#{path} -> ignored."
      next
    end

    LivereloadRails.logger.debug "#{path} -> #{filename}."
    @update[filename]
  end
end

#translate(path) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/livereload_rails/watcher.rb', line 32

def translate(path)
  @matchers.find do |name, matcher|
    if value = matcher.call(path)
      return value
    end
  end
end