Class: Spring::Watcher::Abstract
- Inherits:
-
Object
- Object
- Spring::Watcher::Abstract
- 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
Instance Attribute Summary collapse
-
#directories ⇒ Object
readonly
Returns the value of attribute directories.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#latency ⇒ Object
readonly
Returns the value of attribute latency.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #add(*items) ⇒ Object
-
#initialize(root, latency) ⇒ Abstract
constructor
A new instance of Abstract.
- #mark_stale ⇒ Object
- #on_stale(&block) ⇒ Object
- #restart ⇒ Object
- #stale? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
- #subjects_changed ⇒ Object
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 @listeners = [] end |
Instance Attribute Details
#directories ⇒ Object (readonly)
Returns the value of attribute directories.
15 16 17 |
# File 'lib/spring/watcher/abstract.rb', line 15 def directories @directories end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
15 16 17 |
# File 'lib/spring/watcher/abstract.rb', line 15 def files @files end |
#latency ⇒ Object (readonly)
Returns the value of attribute latency.
15 16 17 |
# File 'lib/spring/watcher/abstract.rb', line 15 def latency @latency end |
#root ⇒ Object (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 49 50 51 52 |
# 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 ⇒ Object
62 63 64 65 66 |
# File 'lib/spring/watcher/abstract.rb', line 62 def mark_stale return if stale? @stale = true @listeners.each(&:call) end |
#on_stale(&block) ⇒ Object
58 59 60 |
# File 'lib/spring/watcher/abstract.rb', line 58 def on_stale(&block) @listeners << block end |
#restart ⇒ Object
68 69 70 71 |
# File 'lib/spring/watcher/abstract.rb', line 68 def restart stop start end |
#stale? ⇒ Boolean
54 55 56 |
# File 'lib/spring/watcher/abstract.rb', line 54 def stale? @stale end |
#start ⇒ Object
73 74 75 |
# File 'lib/spring/watcher/abstract.rb', line 73 def start raise NotImplementedError end |
#stop ⇒ Object
77 78 79 |
# File 'lib/spring/watcher/abstract.rb', line 77 def stop raise NotImplementedError end |
#subjects_changed ⇒ Object
81 82 83 |
# File 'lib/spring/watcher/abstract.rb', line 81 def subjects_changed raise NotImplementedError end |