Class: Listen::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/listen/record/entry.rb,
lib/listen/record.rb,
lib/listen/record/symlink_detector.rb

Defined Under Namespace

Classes: Entry, SymlinkDetector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Record

Returns a new instance of Record.



10
11
12
13
# File 'lib/listen/record.rb', line 10

def initialize(directory)
  @tree = _auto_hash
  @root = directory.to_s
end

Instance Attribute Details

#rootObject (readonly)

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



9
10
11
# File 'lib/listen/record.rb', line 9

def root
  @root
end

Instance Method Details

#add_dir(rel_path) ⇒ Object



15
16
17
18
# File 'lib/listen/record.rb', line 15

def add_dir(rel_path)
  return if [nil, '', '.'].include? rel_path
  @tree[rel_path] ||= {}
end

#buildObject



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

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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/listen/record.rb', line 42

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) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/listen/record.rb', line 30

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) ⇒ Object



25
26
27
28
# File 'lib/listen/record.rb', line 25

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) ⇒ Object



20
21
22
23
# File 'lib/listen/record.rb', line 20

def update_file(rel_path, data)
  dirname, basename = Pathname(rel_path).split.map(&:to_s)
  _fast_update_file(dirname, basename, data)
end