Class: DirChecksum::Base
- Inherits:
-
Object
- Object
- DirChecksum::Base
- Defined in:
- lib/dir_checksum.rb
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Object) checksum_file
readonly
Returns the value of attribute checksum_file.
-
- (Object) checksum_name
readonly
Returns the value of attribute checksum_name.
-
- (Object) dir
readonly
Returns the value of attribute dir.
Instance Method Summary (collapse)
- - (Object) diffs
- - (Boolean) has_checksum_file?
-
- (Base) initialize(*args)
constructor
A new instance of Base.
- - (Object) read_from_file
- - (Object) recursive_checksum(&block)
- - (Object) warn_if_has_checksum_file_and_diff!(&block)
- - (Object) write_to_file!(&block)
Constructor Details
- (Base) initialize(*args)
A new instance of Base
9 10 11 12 13 14 15 |
# File 'lib/dir_checksum.rb', line 9 def initialize(*args) = Hash === args.last ? args.pop : {} @checksum_name = args.shift || [:checksum_name] || :checksum @dir = args.shift || [:dir] || Dir.pwd @checksum_file = args.shift || [:checksum_file] || "#{dir}/.#{checksum_name.to_s.downcase}s" end |
Instance Attribute Details
- (Object) checksum_file (readonly)
Returns the value of attribute checksum_file
7 8 9 |
# File 'lib/dir_checksum.rb', line 7 def checksum_file @checksum_file end |
- (Object) checksum_name (readonly)
Returns the value of attribute checksum_name
7 8 9 |
# File 'lib/dir_checksum.rb', line 7 def checksum_name @checksum_name end |
- (Object) dir (readonly)
Returns the value of attribute dir
7 8 9 |
# File 'lib/dir_checksum.rb', line 7 def dir @dir end |
Instance Method Details
- (Object) diffs
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dir_checksum.rb', line 68 def diffs diffs = [] checks = read_from_file recursive_checksum do |file, check| prev_check = checks.delete(file) if prev_check.nil? diffs << "New file: #{file}" elsif prev_check != check diffs << "File has changed: #{file}" end end checks.each do |file, check| diffs << "Missing file: #{file}" end diffs end |
- (Boolean) has_checksum_file?
26 27 28 |
# File 'lib/dir_checksum.rb', line 26 def has_checksum_file? File.exist?(checksum_file) end |
- (Object) read_from_file
40 41 42 43 44 45 46 |
# File 'lib/dir_checksum.rb', line 40 def read_from_file checks = File.read(checksum_file).lines.map do |line| next if line =~ /^#/ line.chomp.split(/\t/) end Hash[*checks.compact.flatten] end |
- (Object) recursive_checksum(&block)
17 18 19 20 21 22 23 24 |
# File 'lib/dir_checksum.rb', line 17 def recursive_checksum(&block) Dir["#{dir}/**/*"].sort.each do |file| if File.file?(file) and file != checksum_file yield file.sub(%r{^#{dir}/*}, ''), checksum(file) end end nil end |
- (Object) warn_if_has_checksum_file_and_diff!(&block)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dir_checksum.rb', line 48 def warn_if_has_checksum_file_and_diff!(&block) if has_checksum_file? diffs = self.diffs if diffs.empty? # do nothing elsif block_given? yield(diffs) nil else msg = "DIRECTORY #{dir} CONTAINS DIFFERENCES" header = "!" * (msg.length + 6) puts header puts "!! #{msg} !!" puts header diffs.sort.each { |d| puts "!! #{d}" } puts header end end end |
- (Object) write_to_file!(&block)
30 31 32 33 34 35 36 37 38 |
# File 'lib/dir_checksum.rb', line 30 def write_to_file!(&block) File.open(checksum_file, 'w') do |out| header = yield(self) if block_given? out.puts("# #{header}") if header recursive_checksum do |*args| out.puts(args * "\t") end end end |