Module: Manifesto

Defined in:
lib/manifesto.rb

Class Method Summary collapse

Class Method Details

.cache(options = {}) ⇒ Object

Returns an Array of non-hidden files within the ./public directory, unless :directory is specified. The output also includes a computed hash of the files’ contents, unless :compute_hash is set to false.

Examples

Basic usage, list all non-hidden files in ./public and include a computed hash of their contents:

Manifesto.cache

Specify a directory:

Manifesto.cache :directory => './mobile'

Specify a directory and don’t compute the hash:

Manifesto.cache :directory => './mobile', :compute_hash => false


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/manifesto.rb', line 19

def self.cache(options = {})
  directory = options.fetch(:directory, './public')
  compute_hash  = options.fetch(:compute_hash, true)
  validate_arguments(directory, compute_hash)
  manifest = []
  hashes = ''
  Find.find(directory) do |path|
  
    # Only include real files (i.e. not directories, symlinks etc.) and non-hidden
    # files in the manifest.
    if File.file?(path) && File.basename(path)[0,1] != '.'
      manifest << "#{normalize_path(directory, path)}\n"
      hashes += compute_file_contents_hash(path) if compute_hash
    end
  end
  
  # Hash the hashes of each file and output as a comment.
  manifest << "# Hash: #{Digest::MD5.hexdigest(hashes)}\n" if compute_hash
  manifest << "# Generated by manifesto (http://github.com/johntopley/manifesto)\n"
  manifest << "CACHE MANIFEST\n"
  manifest.reverse
end