Class: Lather::Watcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*globs, &callback) ⇒ Watcher

Returns a new instance of Watcher.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
# File 'lib/lather/watcher.rb', line 6

def initialize *globs, &callback
  raise ArgumentError, "need a callback" unless block_given?
  @callback = callback

  @options = { :force => false, :sleep => 1 }
  @options.merge!(globs.pop) if globs.last.is_a? Hash

  @globs = globs.flatten
  @files = find_files
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



3
4
5
# File 'lib/lather/watcher.rb', line 3

def files
  @files
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/lather/watcher.rb', line 4

def options
  @options
end

Instance Method Details

#go!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lather/watcher.rb', line 17

def go!
  @timestamp = Time.now

  @callback[@files.keys] if @options[:force]

  loop do
    unless (changed = update_files_and_timestamp).empty?
      @callback[changed]
    end

    Kernel.sleep @options[:sleep]
  end
end