Sha256: b422bd02b93c25e20741a9923d29add39a60410959e81f159f161e1dc6981fee

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# encoding: utf-8
require 'digest/md5'

# this class handles file checksums
class EasyHtmlGenerator::Checksum
  @file_checksums = {}

  def self.checksum(input)
    Digest::MD5.hexdigest(input)
  end

  def self.file_checksum(file)
    return array_checksum(file) if file.is_a? Array
    return array_checksum(Dir["#{folder}/**/*"]) if File.directory? file
    checksum(File.read(file))
  end

  def self.array_checksum(files)
    tmp = ''
    files.each { |file| tmp += File.read(file) if File.file? file }
    checksum(tmp)
  end

  def self.file_changed?(file, context = '')
    key = build_key(file, context)

    return true unless @file_checksums.key? key

    @file_checksums[key] != file_checksum(file)
  end

  def self.folder_changed?(folder, context = '')
    key = build_key(folder, context)

    return true unless @file_checksums.key? key

    @file_checksums[key] != folder_checksum(folder)
  end

  def self.store_file(file, context = '')
    key = build_key(file, context)

    @file_checksums[key] = file_checksum(file)
  end

  def self.invalidate_file(file, context = '')
    key = build_key(file, context)

    @file_checksums.delete(key)
  end

  def self.invalidate_all()
    @file_checksums = {}
  end

  def self.build_key(file, context = '')
    "#{context}.#{file}"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
easy_html_generator-1.0.6 lib/easy_html_generator/checksum.rb