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.


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

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


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

def root
  @root
end

Instance Method Details

#add_dir(rel_path) ⇒ Object


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

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

#buildObject


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

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


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/listen/record.rb', line 47

def dir_entries(rel_path)
  subtree = if ['', '.'].include? rel_path.to_s
    @tree
  else
    @tree[rel_path.to_s] ||= _auto_hash
    @tree[rel_path.to_s]
  end

  subtree.transform_values do |values|
    # only get data for file entries
    values.key?(:mtime) ? values : {}
  end
end

#file_data(rel_path) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/listen/record.rb', line 35

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


30
31
32
33
# File 'lib/listen/record.rb', line 30

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


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

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