Class: Impression::FileWatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ FileWatcher

Returns a new instance of FileWatcher.



7
8
9
10
11
# File 'lib/impression/file_watcher.rb', line 7

def initialize(spec)
  @notifier = INotify::Notifier.new
  @buffer = []
  setup(spec)
end

Instance Method Details

#each(&block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/impression/file_watcher.rb', line 37

def each(&block)
  @receiver = Fiber.current
  io = @notifier.to_io
  loop do
    io.wait_readable
    @notifier.process
    next if @buffer.empty?

    @buffer.each(&block)
    @buffer.clear
  end
end

#setup(spec) ⇒ Object



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

def setup(spec)
  if File.file?(spec)
    setup_single_file(spec)
  elsif File.directory?(spec)
    setup_directory(spec)
  else
    dir = File.dir_name(spec)
    filename = File.basename(spec)
    if dir =~ /\/\*\*$/
      dir = File.dir_name(dir)
      filename = "**/#{filename}"
    end
    setup_directory(dir, filename)
  end
end

#setup_directory(dir) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/impression/file_watcher.rb', line 29

def setup_directory(dir)
  @notifier.watch(dir, :moved_to, :create, :move, :attrib, :modify, :delete) do |event|
    kind = event.flags.first

    @buffer << [kind, event.absolute_name]
  end
end