Class: Listen::Record
- Inherits:
-
Object
- Object
- Listen::Record
- Includes:
- Celluloid
- Defined in:
- lib/listen/record.rb
Instance Attribute Summary collapse
-
#listener ⇒ Object
TODO: deprecate.
-
#paths ⇒ Object
TODO: deprecate.
Instance Method Summary collapse
- #_auto_hash ⇒ Object private
- #_fast_build(root) ⇒ Object private
- #_fast_unset_path(dir, dirname, basename) ⇒ Object private
- #_fast_update_file(dir, dirname, basename, data) ⇒ Object private
- #add_dir(dir, rel_path) ⇒ Object
- #build ⇒ Object
- #dir_entries(dir, rel_path) ⇒ Object
- #file_data(dir, rel_path) ⇒ Object
-
#initialize(listener) ⇒ Record
constructor
A new instance of Record.
- #unset_path(dir, rel_path) ⇒ Object
- #update_file(dir, rel_path, data) ⇒ Object
Constructor Details
#initialize(listener) ⇒ Record
Returns a new instance of Record.
10 11 12 13 |
# File 'lib/listen/record.rb', line 10 def initialize(listener) @listener = listener @paths = _auto_hash end |
Instance Attribute Details
#listener ⇒ Object
TODO: deprecate
8 9 10 |
# File 'lib/listen/record.rb', line 8 def listener @listener end |
#paths ⇒ Object
TODO: deprecate
8 9 10 |
# File 'lib/listen/record.rb', line 8 def paths @paths end |
Instance Method Details
#_auto_hash ⇒ Object (private)
78 79 80 |
# File 'lib/listen/record.rb', line 78 def _auto_hash Hash.new { |h, k| h[k] = Hash.new } end |
#_fast_build(root) ⇒ Object (private)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/listen/record.rb', line 100 def _fast_build(root) @paths[root] = _auto_hash left = Queue.new left << '.' while !left.empty? dirname = left.pop add_dir(root, dirname) path = ::File.join(root, dirname) current = Dir.entries(path.to_s) - %w(. ..) current.each do |entry| full_path = ::File.join(path, entry) if Dir.exist?(full_path) left << (dirname == '.' ? entry : ::File.join(dirname, entry)) else begin lstat = ::File.lstat(full_path) data = { mtime: lstat.mtime.to_f, mode: lstat.mode } _fast_update_file(root, dirname, entry, data) rescue SystemCallError _fast_unset_path(root, dirname, entry) end end end end end |
#_fast_unset_path(dir, dirname, basename) ⇒ Object (private)
92 93 94 95 96 97 98 |
# File 'lib/listen/record.rb', line 92 def _fast_unset_path(dir, dirname, basename) root = @paths[dir.to_s] # this may need to be reworked to properly remove # entries from a tree, without adding non-existing dirs to the record return unless root.key?(dirname) root[dirname].delete(basename) end |
#_fast_update_file(dir, dirname, basename, data) ⇒ Object (private)
82 83 84 85 86 87 88 89 90 |
# File 'lib/listen/record.rb', line 82 def _fast_update_file(dir, dirname, basename, data) root = @paths[dir.to_s] if [nil, '', '.'].include? dirname root[basename] = (root[basename] || {}).merge(data) else root[dirname] ||= {} root[dirname][basename] = (root[dirname][basename] || {}).merge(data) end end |
#add_dir(dir, rel_path) ⇒ Object
15 16 17 18 |
# File 'lib/listen/record.rb', line 15 def add_dir(dir, rel_path) return if [nil, '', '.'].include? rel_path @paths[dir.to_s][rel_path] ||= {} end |
#build ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/listen/record.rb', line 61 def build start = Time.now.to_f @paths = _auto_hash # TODO: refactor this out (1 Record = 1 watched dir) listener.directories.each do |directory| _fast_build(directory.to_s) end Celluloid.logger.info "Record.build took #{Time.now.to_f - start} seconds" rescue Celluloid.logger.warn "build crashed: #{$!.inspect}" raise end |
#dir_entries(dir, rel_path) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/listen/record.rb', line 45 def dir_entries(dir, rel_path) tree = if [nil, '', '.'].include? rel_path.to_s @paths[dir.to_s] else @paths[dir.to_s][rel_path.to_s] ||= _auto_hash @paths[dir.to_s][rel_path.to_s] end result = {} tree.each do |key, values| # only get data for file entries result[key] = values.key?(:mtime) ? values : {} end result end |
#file_data(dir, rel_path) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/listen/record.rb', line 32 def file_data(dir, rel_path) root = @paths[dir.to_s] dirname, basename = Pathname(rel_path).split.map(&:to_s) if [nil, '', '.'].include? dirname root[basename] ||= {} root[basename].dup else root[dirname] ||= {} root[dirname][basename] ||= {} root[dirname][basename].dup end end |
#unset_path(dir, rel_path) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/listen/record.rb', line 25 def unset_path(dir, rel_path) dirname, basename = Pathname(rel_path).split.map(&:to_s) @paths[dir.to_s][dirname] ||= {} _fast_unset_path(dir, dirname, basename) end |
#update_file(dir, rel_path, data) ⇒ Object
20 21 22 23 |
# File 'lib/listen/record.rb', line 20 def update_file(dir, rel_path, data) dirname, basename = Pathname(rel_path).split.map(&:to_s) _fast_update_file(dir, dirname, basename, data) end |