Class: Relinker::Linker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Linker.



6
7
8
9
10
# File 'lib/relinker/linker.rb', line 6

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

Instance Attribute Details

#cache_fileObject (readonly)

Returns the value of attribute cache_file.



4
5
6
# File 'lib/relinker/linker.rb', line 4

def cache_file
  @cache_file
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/relinker/linker.rb', line 4

def options
  @options
end

Instance Method Details

#loop_filesObject



32
33
34
35
36
37
38
# File 'lib/relinker/linker.rb', line 32

def loop_files
  File.open(@cache_file, "r") do |cache|
    cache.each do |line|
      yield line.split("$$$\t").map(&:strip)
    end
  end
end

#loop_identicals(&block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/relinker/linker.rb', line 24

def loop_identicals(&block)
  loop_files do |checksum, file|
    publish_state(&block) if checksum != @prev_checksum
    update_state(checksum, file)
  end
  publish_state(&block)
end


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/relinker/linker.rb', line 12

def relink_identicals
  loop_identicals do |checksum, identicals|
    next if identicals.count <= 1
    origin = identicals.shift
    identicals.each do |identical|
      # Check if file exists for the case when it was removed
      # while the script was running
      system("ln -f #{origin} #{identical}") if File.exist?(origin)
    end
  end
end