Class: Relinker::Discoverer

Inherits:
Object
  • Object
show all
Defined in:
lib/relinker/discoverer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_file, options = {}) ⇒ Discoverer

Returns a new instance of Discoverer.



9
10
11
12
# File 'lib/relinker/discoverer.rb', line 9

def initialize(cache_file, options = {})
  @cache_file = cache_file
  @options = options
end

Instance Attribute Details

#cache_fileObject (readonly)

Returns the value of attribute cache_file.



7
8
9
# File 'lib/relinker/discoverer.rb', line 7

def cache_file
  @cache_file
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/relinker/discoverer.rb', line 7

def options
  @options
end

Instance Method Details

#collect(base_dir) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/relinker/discoverer.rb', line 14

def collect(base_dir)
  File.open(cache_file, "a+") do |cache|
    discover(base_dir) do |file, checksum|
      cache.write("#{checksum}$$$\t#{file}\n")
    end
  end
  resort_cache
end

#discover(base_dir) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/relinker/discoverer.rb', line 28

def discover(base_dir)
  children = Pathname.new(base_dir).children
  children.each do |child|
    next if child.symlink?
    if child.directory?
      discover(child) { |file| yield(file, checksum(file)) }
    else
      yield(child, checksum(child))
    end
  end
end

#list(base_dir) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/relinker/discoverer.rb', line 40

def list(base_dir)
  files = []
  discover(base_dir) do |file, check|
    files << { path: file.to_s, checksum: check }
  end
  files
end

#resort_cacheObject



23
24
25
26
# File 'lib/relinker/discoverer.rb', line 23

def resort_cache
  system("cat #{cache_file} | sort | uniq | cat > #{cache_file}.tmp \
          && mv #{cache_file}.tmp #{cache_file}")
end