class JsDuck::Util::MD5

Helper to rename files so that the MD5 hash of their contents is placed into their name.

Public Instance Methods

rename(fname) click to toggle source

Calculates MD5 hash of a file and renames the file to contain the hash inside the filename. Returns the new name of the file.

# File lib/jsduck/util/md5.rb, line 14
def rename(fname)
  hash = Digest::MD5.file(fname).hexdigest
  hashed_name = inject_hash_to_filename(fname, hash)
  File.rename(fname, hashed_name)
  return hashed_name
end

Private Instance Methods

inject_hash_to_filename(fname, hash) click to toggle source

Given filename “foo/bar.js” and hash “HASH” produces “foo/bar-HASH.js”

# File lib/jsduck/util/md5.rb, line 24
def inject_hash_to_filename(fname, hash)
  parts = File.basename(fname).split(/\./)
  parts[0] += "-" + hash
  File.dirname(fname) + "/" + parts.join(".")
end