Class: Spectator::PathWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/spectator/path_watcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ PathWatcher

Returns a new instance of PathWatcher.



8
9
10
11
# File 'lib/spectator/path_watcher.rb', line 8

def initialize(config)
  @config = config
  @queue = Queue.new
end

Instance Method Details

#has_changed_files?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/spectator/path_watcher.rb', line 13

def has_changed_files?
  queue.size > 0
end

#listenerObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spectator/path_watcher.rb', line 35

def listener
  @listener ||= begin
    listener = Listen.to(Dir.pwd, :relative_paths => true)
    p [:watching, config.base_dir_regexp]
    listener = listener.filter %r{^(#{config.base_dir_regexp}|#{config.spec_dir_regexp})/}
    listener = listener.change do  |modified, added, removed|
      p ['modified, added, removed', modified, added, removed]
      files = [modified, added].flatten
      files.each { |relative| queue.push relative }
      p on_change
      on_change.call(files) if on_change && files.any?
    end
  end
end

#on_change(&block) ⇒ Object



17
18
19
20
# File 'lib/spectator/path_watcher.rb', line 17

def on_change &block
  @on_change = block if block_given?
  @on_change
end

#pop_filesObject



22
23
24
25
26
# File 'lib/spectator/path_watcher.rb', line 22

def pop_files
  files = []
  queue.size.times { files << queue.pop }
  files
end

#watch_paths!Object



28
29
30
# File 'lib/spectator/path_watcher.rb', line 28

def watch_paths!
  listener.start
end