Class: CheckSumSerializer

Inherits:
Object
  • Object
show all
Defined in:
CheckSumCalculator.rb

Constant Summary

@@hash_filename =
"wm_bin_hash.txt"
@@hash_filepath =
nil

Instance Method Summary (collapse)

Constructor Details

- (CheckSumSerializer) initialize(filepath)

Returns a new instance of CheckSumSerializer



9
10
11
# File 'CheckSumCalculator.rb', line 9

def initialize(filepath)
  @@hash_filepath = File.join(filepath, @@hash_filename)
end

Instance Method Details

- (Object) cansave



52
53
54
# File 'CheckSumCalculator.rb', line 52

def cansave
  return !File.exists?(@@hash_filepath)
end

- (Object) load



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'CheckSumCalculator.rb', line 27

def load()
  loaded_hash = Array.new

  File.readlines(@@hash_filepath).each do |line|
    lines = line.each_line('|').to_a

    next if lines.length < 2

    name = lines[0].chomp('|')
    md5  = lines[1].chomp('|')
    name = name.chomp
    md5  = md5.chomp

    line_hash = Hash.new
    line_hash["name"] = name.to_s
    line_hash["md5"]  = md5.to_s

    loaded_hash << line_hash
  end

  puts "loaded hashes for " + loaded_hash.length.to_s + " files"

  loaded_hash
end

- (Object) save(hashes)



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'CheckSumCalculator.rb', line 13

def save(hashes) 
  puts "save hashes for " + hashes.length.to_s + " files"

  FileUtils.rm_f @@hash_filepath

  f = File.new(@@hash_filepath, "w+")

  hashes.each { |file|
    f.puts file["name"].to_s + "|" + file["md5"].to_s
  }

  f.close
end