class Spring::Watcher::Abstract

A user of a watcher can use IO.select to wait for changes:

watcher = MyWatcher.new(root, latency)
IO.select([watcher]) # watcher is running in background
watcher.stale? # => true

Attributes

directories[R]
files[R]
latency[R]
root[R]

Public Class Methods

new(root, latency) click to toggle source
Calls superclass method
# File lib/spring/watcher/abstract.rb, line 17
def initialize(root, latency)
  super()

  @root        = File.realpath(root)
  @latency     = latency
  @files       = Set.new
  @directories = Set.new
  @stale       = false
  @listeners   = []
end

Public Instance Methods

add(*items) click to toggle source
# File lib/spring/watcher/abstract.rb, line 28
def add(*items)
  items = items.flatten.map do |item|
    item = Pathname.new(item)

    if item.relative?
      Pathname.new("#{root}/#{item}")
    else
      item
    end
  end

  items = items.select(&:exist?)

  synchronize {
    items.each do |item|
      if item.directory?
        directories << item.realpath.to_s
      else
        files << item.realpath.to_s
      end
    end

    subjects_changed
  }
end
mark_stale() click to toggle source
# File lib/spring/watcher/abstract.rb, line 62
def mark_stale
  return if stale?
  @stale = true
  @listeners.each(&:call)
end
on_stale(&block) click to toggle source
# File lib/spring/watcher/abstract.rb, line 58
def on_stale(&block)
  @listeners << block
end
restart() click to toggle source
# File lib/spring/watcher/abstract.rb, line 68
def restart
  stop
  start
end
stale?() click to toggle source
# File lib/spring/watcher/abstract.rb, line 54
def stale?
  @stale
end
start() click to toggle source
# File lib/spring/watcher/abstract.rb, line 73
def start
  raise NotImplementedError
end
stop() click to toggle source
# File lib/spring/watcher/abstract.rb, line 77
def stop
  raise NotImplementedError
end
subjects_changed() click to toggle source
# File lib/spring/watcher/abstract.rb, line 81
def subjects_changed
  raise NotImplementedError
end