Class: Dirwatch::Watcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Watcher

Returns a new instance of Watcher.



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

def initialize options
  @options = options
  @settings = Settings.from_options @options

  return unless options.daemonize
  Process.daemon true, true
  puts "running in the background... [#{Process.pid}]"
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#settingsObject (readonly)

Returns the value of attribute settings.



6
7
8
# File 'lib/dirwatch/watcher.rb', line 6

def settings
  @settings
end

Instance Method Details

#filesObject



43
44
45
# File 'lib/dirwatch/watcher.rb', line 43

def files
  Dir[File.join options.directory, '**', options.file_match]
end

#startObject



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

def start
  raise 'already started' if @threads
  @threads = []
  @stop = false

  Thread.abort_on_exception = true
  @settings.by_interval do |interval, watch_settings|
    if @options.verbose
      watch_settings.each {|ws| puts "Watching #{ws}" }
    end
    @threads << Thread.new do
      run interval, watch_settings
    end
  end
end

#stopObject



37
38
39
40
41
# File 'lib/dirwatch/watcher.rb', line 37

def stop
  raise 'not started' unless @threads
  @stop = true
  wait_for_stop
end

#wait_for_stopObject



33
34
35
# File 'lib/dirwatch/watcher.rb', line 33

def wait_for_stop
  @threads.each(&:join)
end