Class: Watchly::Watcher
- Inherits:
-
Object
- Object
- Watchly::Watcher
- Defined in:
- lib/watchly/watcher.rb
Constant Summary collapse
- DEFAULT_INTERVAL =
1.0
Instance Attribute Summary collapse
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#stopped ⇒ Object
readonly
Returns the value of attribute stopped.
-
#targets ⇒ Object
(also: #globs)
readonly
Returns the value of attribute targets.
Instance Method Summary collapse
-
#initialize(*targets, interval: nil) ⇒ Watcher
constructor
A new instance of Watcher.
- #on_change ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(*targets, interval: nil) ⇒ Watcher
Returns a new instance of Watcher.
8 9 10 11 12 |
# File 'lib/watchly/watcher.rb', line 8 def initialize(*targets, interval: nil) @targets = targets @interval = interval || DEFAULT_INTERVAL @stopped = false end |
Instance Attribute Details
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
5 6 7 |
# File 'lib/watchly/watcher.rb', line 5 def interval @interval end |
#stopped ⇒ Object (readonly)
Returns the value of attribute stopped.
5 6 7 |
# File 'lib/watchly/watcher.rb', line 5 def stopped @stopped end |
#targets ⇒ Object (readonly) Also known as: globs
Returns the value of attribute targets.
5 6 7 |
# File 'lib/watchly/watcher.rb', line 5 def targets @targets end |
Instance Method Details
#on_change ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/watchly/watcher.rb', line 16 def on_change raise ArgumentError, 'Block required' unless block_given? previous = Snapshot.new targets until stopped sleep interval current = Snapshot.new targets next if previous == current changes = previous.diff current yield changes previous = current end end |
#stop ⇒ Object
14 |
# File 'lib/watchly/watcher.rb', line 14 def stop = @stopped = true |