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.



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

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.



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

def cache_file
  @cache_file
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#collect(base_dir) ⇒ Object



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

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



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

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



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

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

#resort_cacheObject



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

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