Sha256: 97a1233fad10a6129dc2b942577daeef3ff71930cb68e491cc8bf3599d2f2413

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'yaml'
require 'yaml/store'
require 'digest/md5'

module Cbt
  class Digester

    # initialize from the configuration
    def initialize store_file
      @store_file = store_file
      @store = YAML::Store.new @store_file
    end

    def file_digest file
      return "" unless File.exists? file
      d = Digest::MD5.hexdigest(File.read(file))
    end

    # add file copy history
    def add_history file, digest = nil
      digest = file_digest file unless digest
      @store.transaction do
        @store[file] = Array.new unless @store[file]
        @store[file] << {
          'datetime' => DateTime.now.strftime("%F %T"),
          'digest' => digest
        }
      end
    end

    # return 'datetime' => '', 'digest' => '' or nil for no store
    def last_change file
      @store.transaction do      
        @store[file][-1] if @store[file]
      end
    end

    # if the digest is different from last time
    def changed? file, digest = nil
      digest = file_digest file unless digest
      @store.transaction do
        not (@store[file] and @store[file][-1] and @store[file][-1]['digest'] == digest)
      end
    end

    # if the current digest exists in file store
    def known? file, digest = nil
      digest = file_digest file unless digest
      @store.transaction do      
        return false unless @store[file]
        @store[file].each do |h|
          return true if h['digest'] == digest
        end
      end
      false
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cbt-0.0.1 lib/cbt/digester.rb