Class: Relinker::Linker
- Inherits:
-
Object
- Object
- Relinker::Linker
- Defined in:
- lib/relinker/linker.rb
Instance Attribute Summary collapse
-
#cache_file ⇒ Object
readonly
Returns the value of attribute cache_file.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(cache_file, options = {}) ⇒ Linker
constructor
A new instance of Linker.
- #loop_files ⇒ Object
- #loop_identicals(&block) ⇒ Object
- #relink_identicals ⇒ Object
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, = {}) @cache_file = cache_file @options = reset_state end |
Instance Attribute Details
#cache_file ⇒ Object (readonly)
Returns the value of attribute cache_file.
4 5 6 |
# File 'lib/relinker/linker.rb', line 4 def cache_file @cache_file end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/relinker/linker.rb', line 4 def @options end |
Instance Method Details
#loop_files ⇒ Object
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 |
#relink_identicals ⇒ Object
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 |