Class: Spring::Watcher::Abstract

Inherits:
Object
  • Object
show all
Includes:
Mutex_m
Defined in:
lib/spring/watcher/abstract.rb

Overview

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

Direct Known Subclasses

Listen, Polling

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, latency) ⇒ Abstract

Returns a new instance of Abstract.



17
18
19
20
21
22
23
24
25
26
# 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
  @io_listener = nil
end

Instance Attribute Details

#directoriesObject (readonly)

Returns the value of attribute directories.



15
16
17
# File 'lib/spring/watcher/abstract.rb', line 15

def directories
  @directories
end

#filesObject (readonly)

Returns the value of attribute files.



15
16
17
# File 'lib/spring/watcher/abstract.rb', line 15

def files
  @files
end

#latencyObject (readonly)

Returns the value of attribute latency.



15
16
17
# File 'lib/spring/watcher/abstract.rb', line 15

def latency
  @latency
end

#rootObject (readonly)

Returns the value of attribute root.



15
16
17
# File 'lib/spring/watcher/abstract.rb', line 15

def root
  @root
end

Instance Method Details

#add(*items) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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.each do |item|
    if item.directory?
      directories << item.realpath.to_s
    else
      files << item.realpath.to_s
    end
  end

  subjects_changed
end

#mark_staleObject



54
55
56
57
# File 'lib/spring/watcher/abstract.rb', line 54

def mark_stale
  @stale = true
  @io_listener.write "." if @io_listener
end

#restartObject



65
66
67
68
# File 'lib/spring/watcher/abstract.rb', line 65

def restart
  stop
  start
end

#stale?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/spring/watcher/abstract.rb', line 50

def stale?
  @stale
end

#startObject

Raises:

  • (NotImplementedError)


70
71
72
# File 'lib/spring/watcher/abstract.rb', line 70

def start
  raise NotImplementedError
end

#stopObject

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/spring/watcher/abstract.rb', line 74

def stop
  raise NotImplementedError
end

#subjects_changedObject

Raises:

  • (NotImplementedError)


78
79
80
# File 'lib/spring/watcher/abstract.rb', line 78

def subjects_changed
  raise NotImplementedError
end

#to_ioObject



59
60
61
62
63
# File 'lib/spring/watcher/abstract.rb', line 59

def to_io
  read, write = IO.pipe
  @io_listener = write
  read
end