Class: Listen::Directory

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

Class Method Summary collapse

Class Method Details

._async_changes(dir, path, queue, previous, options) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/listen/directory.rb', line 49

def self._async_changes(dir, path, queue, previous, options)
  previous.each do |entry, data|
    # TODO: this is a hack with insufficient testing
    type = data.key?(:mtime) ? :file : :dir
    _change(queue, type, dir, (Pathname(path) + entry).to_s, options)
  end
end

._change(queue, type, dir, path, options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/listen/directory.rb', line 57

def self._change(queue, type, dir, path, options)
  return queue.change(type, dir, path, options) if type == :dir

  # Minor param cleanup for tests
  # TODO: use a dedicated Event class
  opts = options.dup
  opts.delete(:recursive)
  if opts.empty?
    queue.change(type, dir, path)
  else
    queue.change(type, dir, path, opts)
  end
end

._log(type, message) ⇒ Object



71
72
73
# File 'lib/listen/directory.rb', line 71

def self._log(type, message)
  Celluloid.logger.send(type, message)
end

.scan(queue, sync_record, dir, rel_path, options) ⇒ Object



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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/listen/directory.rb', line 5

def self.scan(queue, sync_record, dir, rel_path, options)
  return unless (record = sync_record.async)

  previous = sync_record.dir_entries(dir, rel_path)

  record.add_dir(dir, rel_path)

  # TODO: use children(with_directory: false)
  path = dir + rel_path
  current = Set.new(path.children)

  if options[:silence]
    _log :debug, "Recording: #{rel_path}: #{options.inspect}"\
      " [#{previous.inspect}] -> (#{current.inspect})"
  else
    _log :debug, "Scanning: #{rel_path}: #{options.inspect}"\
      " [#{previous.inspect}] -> (#{current.inspect})"
  end

  current.each do |full_path|
    type = full_path.directory? ? :dir : :file
    item_rel_path = full_path.relative_path_from(dir).to_s
    _change(queue, type, dir, item_rel_path, options)
  end

  # TODO: this is not tested properly
  previous.reject! { |entry, _| current.include? path + entry }

  _async_changes(dir, rel_path, queue, previous, options)

rescue Errno::ENOENT
  record.unset_path(dir, rel_path)
  _async_changes(dir, rel_path, queue, previous, options)

rescue Errno::ENOTDIR
  # TODO: path not tested
  record.unset_path(dir, rel_path)
  _async_changes(dir, path, queue, previous, options)
  _change(queue, :file, dir, rel_path, options)
rescue
  _log :warn, "scanning DIED: #{$!}:#{$@.join("\n")}"
  raise
end