Class: Whisper::DirScanner

Inherits:
Object
  • Object
show all
Includes:
Dependency, Loggy
Defined in:
lib/whisper/dir_scanner.rb

Overview

watches a directory tree, keeps a list of filenames, reports modifications, additions and removals. scans at most every min_scan_interval seconds.

TODO: rename to cacheddir

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dependency

#content, #dependency_init, #refresh!

Constructor Details

#initialize(path, filename_regex) ⇒ DirScanner

Returns a new instance of DirScanner.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/whisper/dir_scanner.rb', line 16

def initialize path, filename_regex
  @path = path
  @filename_regex = filename_regex
  @last_scan = Time.at 0

  @files = {}
  @added, @removed, @updated = [], [], []

  DirScanner.min_scan_interval ||= 30
  dependency_init
end

Class Attribute Details

.min_scan_intervalObject

Returns the value of attribute min_scan_interval.



14
15
16
# File 'lib/whisper/dir_scanner.rb', line 14

def min_scan_interval
  @min_scan_interval
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



30
31
32
# File 'lib/whisper/dir_scanner.rb', line 30

def path
  @path
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



30
31
32
# File 'lib/whisper/dir_scanner.rb', line 30

def timestamp
  @timestamp
end

Instance Method Details

#build(old) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/whisper/dir_scanner.rb', line 35

def build old
  #debug "recording changes for myself"
  rescan!
  added, removed, updated = @added, @removed, @updated
  added.each { |fn| @files[fn] = true }
  removed.each { |fn| @files.delete fn }
  @added, @removed, @updated = [], [], []
  [added, removed, updated]
end

#checkObject



45
# File 'lib/whisper/dir_scanner.rb', line 45

def check; rescan! end

#dependenciesObject



28
# File 'lib/whisper/dir_scanner.rb', line 28

def dependencies; [] end

#diffObject

returns the diffs since the last time diff was called



33
# File 'lib/whisper/dir_scanner.rb', line 33

def diff; content end

#rescan!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/whisper/dir_scanner.rb', line 47

def rescan!
  return false unless @last_scan + DirScanner.min_scan_interval < Time.now
  @added, @removed, @updated = [], [], []

  current = {}
  Find.find(@path) do |f|
    next unless f =~ @filename_regex
    current[f] = true
  end

  current.keys.each do |fn|
    if @files[fn]
      @updated << fn if File.mtime(fn) > @last_scan
      next
    end
    @added << fn
  end

  @files.keys.each do |fn|
    next if current[fn]
    @removed << fn
  end

  debug "found #{@added.size} added, #{@removed.size} removed, and #{@updated.size} modified files in #{@path}"
  @last_scan = Time.now
  !@added.empty? || !@removed.empty? || !@updated.empty?
end