Class: FileWatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileWatcher

Returns a new instance of FileWatcher.



4
5
6
7
# File 'lib/file_watcher.rb', line 4

def initialize(path)
  @path = path
  @listener = nil
end

Instance Method Details

#onlyRegObject



29
30
31
# File 'lib/file_watcher.rb', line 29

def onlyReg
  /\.m$/
end

#startWatcherObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/file_watcher.rb', line 9

def startWatcher
  if File.directory?(@path) == false
    @path = File.dirname(@path)
    if File.directory?(@path)
      puts "watcher path not exist: #{@path}"
      return
    end
  end

  @listener = Listen.to(@path, only: onlyReg) do |modified, added, removed|
    modified.each { |path| path.force_encoding('utf-8') }
    yield(modified) unless modified.empty?
    # added.each { |path| path.force_encoding('utf-8') }
    # removed.each { |path| path.force_encoding('utf-8') }
  end

  @listener.start
  sleep
end