Class: FeatherWatch::Core::DarwinWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/feather_watch/core/darwin_watcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(directories, callback, verbose = false, silence_exceptions = false) ⇒ DarwinWatcher

Returns a new instance of DarwinWatcher.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/feather_watch/core/darwin_watcher.rb', line 3

def initialize(directories, callback, verbose= false, silence_exceptions= false)
	@verbose = verbose
	@silence_exceptions = silence_exceptions
	puts "Initializing mac watcher" if @verbose
	@fs_event = FSEvent.new
	options = { :no_defer => true,
				:file_events => true }

	@fs_event.watch directories, options do |changed_files|
		changed_files.each do |f|
			begin
				if File.file?(f)
					puts "Change on file: #{f}" if @verbose
					callback.call({status: :modified, file: f, event: f})
				else
					puts "Removed file: #{f}" if @verbose
					callback.call({status: :removed, file: f, event: f})
				end
			rescue Exception => e
				unless @silence_exceptions
					STDERR.puts "----------------------------"
					STDERR.puts "Error in Feather Watch callback"
					STDERR.puts "Message: #{e.message}"
					STDERR.puts "backtrace: #{e.backtrace * "\n\t >"}"
				end
			end
		end
	end
end

Instance Method Details

#startObject



33
34
35
36
37
38
# File 'lib/feather_watch/core/darwin_watcher.rb', line 33

def start
	puts "Starting mac watcher" if @verbose
	Thread.new do
		@fs_event.run
	end
end

#stopObject



40
41
42
43
# File 'lib/feather_watch/core/darwin_watcher.rb', line 40

def stop
	puts "Stopping mac watcher" if @verbose
	@fs_event.stop
end