Class: Watchly::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/watchly/watcher.rb

Constant Summary collapse

DEFAULT_INTERVAL =
1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#intervalObject (readonly)

Returns the value of attribute interval.



5
6
7
# File 'lib/watchly/watcher.rb', line 5

def interval
  @interval
end

#stoppedObject (readonly)

Returns the value of attribute stopped.



5
6
7
# File 'lib/watchly/watcher.rb', line 5

def stopped
  @stopped
end

#targetsObject (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_changeObject

Raises:

  • (ArgumentError)


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

#stopObject



14
# File 'lib/watchly/watcher.rb', line 14

def stop = @stopped = true