class Listen::Record

@private api

@private api

Attributes

root[R]

TODO: one Record object per watched directory? TODO: deprecate

tree[R]

Public Class Methods

new(directory) click to toggle source
# File lib/listen/record.rb, line 11
def initialize(directory)
  @tree = _auto_hash
  @root = directory.to_s
end

Public Instance Methods

add_dir(rel_path) click to toggle source
# File lib/listen/record.rb, line 16
def add_dir(rel_path)
  return if [nil, '', '.'].include? rel_path
  @tree[rel_path] ||= {}
end
build() click to toggle source
# File lib/listen/record.rb, line 60
def build
  @tree = _auto_hash
  # TODO: test with a file name given
  # TODO: test other permissions
  # TODO: test with mixed encoding
  symlink_detector = SymlinkDetector.new
  remaining = ::Queue.new
  remaining << Entry.new(root, nil, nil)
  _fast_build_dir(remaining, symlink_detector) until remaining.empty?
end
dir_entries(rel_path) click to toggle source
# File lib/listen/record.rb, line 43
def dir_entries(rel_path)
  subtree =
    if [nil, '', '.'].include? rel_path.to_s
      tree
    else
      tree[rel_path.to_s] ||= _auto_hash
      tree[rel_path.to_s]
    end

  result = {}
  subtree.each do |key, values|
    # only get data for file entries
    result[key] = values.key?(:mtime) ? values : {}
  end
  result
end
file_data(rel_path) click to toggle source
# File lib/listen/record.rb, line 31
def file_data(rel_path)
  dirname, basename = Pathname(rel_path).split.map(&:to_s)
  if [nil, '', '.'].include? dirname
    tree[basename] ||= {}
    tree[basename].dup
  else
    tree[dirname] ||= {}
    tree[dirname][basename] ||= {}
    tree[dirname][basename].dup
  end
end
unset_path(rel_path) click to toggle source
# File lib/listen/record.rb, line 26
def unset_path(rel_path)
  dirname, basename = Pathname(rel_path).split.map(&:to_s)
  _fast_unset_path(dirname, basename)
end
update_file(rel_path, data) click to toggle source
# File lib/listen/record.rb, line 21
def update_file(rel_path, data)
  dirname, basename = Pathname(rel_path).split.map(&:to_s)
  _fast_update_file(dirname, basename, data)
end

Private Instance Methods

_auto_hash() click to toggle source
# File lib/listen/record.rb, line 73
def _auto_hash
  Hash.new { |h, k| h[k] = Hash.new }
end
_fast_build_dir(remaining, symlink_detector) click to toggle source
# File lib/listen/record.rb, line 100
def _fast_build_dir(remaining, symlink_detector)
  entry = remaining.pop
  children = entry.children # NOTE: children() implicitly tests if dir
  symlink_detector.verify_unwatched!(entry)
  children.each { |child| remaining << child }
  add_dir(entry.record_dir_key)
rescue Errno::ENOTDIR
  _fast_try_file(entry)
rescue SystemCallError, SymlinkDetector::Error
  _fast_unset_path(entry.relative, entry.name)
end
_fast_try_file(entry) click to toggle source
# File lib/listen/record.rb, line 112
def _fast_try_file(entry)
  _fast_update_file(entry.relative, entry.name, entry.meta)
rescue SystemCallError
  _fast_unset_path(entry.relative, entry.name)
end
_fast_unset_path(dirname, basename) click to toggle source
# File lib/listen/record.rb, line 88
def _fast_unset_path(dirname, basename)
  # this may need to be reworked to properly remove
  # entries from a tree, without adding non-existing dirs to the record
  if [nil, '', '.'].include? dirname
    return unless tree.key?(basename)
    tree.delete(basename)
  else
    return unless tree.key?(dirname)
    tree[dirname].delete(basename)
  end
end
_fast_update_file(dirname, basename, data) click to toggle source
# File lib/listen/record.rb, line 79
def _fast_update_file(dirname, basename, data)
  if [nil, '', '.'].include? dirname
    tree[basename] = (tree[basename] || {}).merge(data)
  else
    tree[dirname] ||= {}
    tree[dirname][basename] = (tree[dirname][basename] || {}).merge(data)
  end
end