Class: RBWatch::FileScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/rbwatch/file_scanner.rb

Defined Under Namespace

Classes: ChangeSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ FileScanner

Returns a new instance of FileScanner.



30
31
32
# File 'lib/rbwatch/file_scanner.rb', line 30

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



28
29
30
# File 'lib/rbwatch/file_scanner.rb', line 28

def configuration
  @configuration
end

Instance Method Details

#countObject



70
71
72
# File 'lib/rbwatch/file_scanner.rb', line 70

def count
  watchable_files.length
end

#diff(previous_snapshot, current_snapshot = scan) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rbwatch/file_scanner.rb', line 50

def diff(previous_snapshot, current_snapshot = scan)
  previous_snapshot ||= {}
  current_snapshot ||= {}

  previous_paths = previous_snapshot.keys
  current_paths = current_snapshot.keys

  added = current_paths - previous_paths
  removed = previous_paths - current_paths
  modified = (previous_paths & current_paths).select do |path|
    previous_snapshot[path] != current_snapshot[path]
  end

  ChangeSet.new(
    added_paths: added.sort,
    removed_paths: removed.sort,
    modified_paths: modified.sort
  )
end

#scanObject



42
43
44
45
46
47
48
# File 'lib/rbwatch/file_scanner.rb', line 42

def scan
  watchable_files.each_with_object({}) do |path, snapshot|
    snapshot[path] = mtime_for(path)
  rescue Errno::ENOENT
    next
  end
end

#watchable_filesObject



34
35
36
37
38
39
40
# File 'lib/rbwatch/file_scanner.rb', line 34

def watchable_files
  files = configuration.watch_paths.flat_map do |root|
    Dir.glob(File.join(root, "**", "*"))
  end

  files.uniq.select { |path| watchable_file?(path) }.map { |path| normalize_path(path) }.sort
end