Class: Spring::Watcher::Listen

Inherits:
Abstract
  • Object
show all
Defined in:
lib/spring/watcher/listen.rb

Instance Attribute Summary collapse

Attributes inherited from Abstract

#directories, #files, #latency, #root

Instance Method Summary collapse

Methods inherited from Abstract

#add, #initialize, #mark_stale, #restart, #stale?, #to_io

Constructor Details

This class inherits a constructor from Spring::Watcher::Abstract

Instance Attribute Details

#listenerObject (readonly)

Returns the value of attribute listener.



8
9
10
# File 'lib/spring/watcher/listen.rb', line 8

def listener
  @listener
end

Instance Method Details

#base_directoriesObject



44
45
46
47
48
49
# File 'lib/spring/watcher/listen.rb', line 44

def base_directories
  ([root] +
    files.reject       { |f| f.start_with? root }.map { |f| File.expand_path("#{f}/..") } +
    directories.reject { |d| d.start_with? root }
  ).uniq
end

#changed(modified, added, removed) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/spring/watcher/listen.rb', line 36

def changed(modified, added, removed)
  synchronize do
    if (modified + added + removed).any? { |f| watching? f }
      mark_stale
    end
  end
end

#startObject



10
11
12
13
14
15
16
17
# File 'lib/spring/watcher/listen.rb', line 10

def start
  unless @listener
    @listener = ::Listen.to(*base_directories, relative_paths: false)
    @listener.latency(latency)
    @listener.change(&method(:changed))
    @listener.start
  end
end

#stopObject



19
20
21
22
23
24
# File 'lib/spring/watcher/listen.rb', line 19

def stop
  if @listener
    @listener.stop
    @listener = nil
  end
end

#subjects_changedObject



26
27
28
29
30
# File 'lib/spring/watcher/listen.rb', line 26

def subjects_changed
  if @listener && @listener.directories.sort != base_directories.sort
    restart
  end
end

#watching?(file) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/spring/watcher/listen.rb', line 32

def watching?(file)
  files.include?(file) || file.start_with?(*directories)
end