Module: Statica

Extended by:
Statica
Included in:
Statica
Defined in:
lib/statica.rb

Defined Under Namespace

Classes: ResourceNotFoundError

Constant Summary collapse

VERSION =

Statica version string

'0.3.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#root_dirObject

Fully qualified path to the root directory of the static assets



13
14
15
# File 'lib/statica.rb', line 13

def root_dir
  @root_dir
end

Instance Method Details

#cached_digestHash

Current cache

Returns:

  • (Hash)

    Current cached_digest hash; initializes to empty hash



32
33
34
# File 'lib/statica.rb', line 32

def cached_digest
  @cached_digest ||= {}
end

#digestDigest::Base

Current digest

Returns:

  • (Digest::Base)

    Digest::Base implementation instance



25
26
27
# File 'lib/statica.rb', line 25

def digest
  (@digest || ::Digest::SHA256).new
end

#digest_class=(klass) ⇒ Object

Set digest class

Parameters:

  • klass (Digest::Base)

    Digest::Base implementation



18
19
20
# File 'lib/statica.rb', line 18

def digest_class=(klass)
  @digest = klass
end

#digest_for(path) ⇒ String

Calculate the hex digest for the given resource

Parameters:

  • Typically (String)

    a resource such as /js/application.js or /css/home-style.css

Returns:

  • (String)

    Digest calculated on the contents of the path

Raises:



62
63
64
65
66
67
68
69
70
# File 'lib/statica.rb', line 62

def digest_for(path)
  return cached_digest[path] if cached_digest.has_key?(path)
  path.chop! if path.end_with?("/")
  fname = File.join(root_dir, path)

  raise ResourceNotFoundError, "#{fname} not found!" unless File.exist?(fname)

  cached_digest[path] = digest.file(fname).hexdigest
end

#digest_url(url) ⇒ String

Append digest to url

Parameters:

  • Typically (String)

    a resource such as /js/application.js or /css/home-style.css

Returns:

  • (String)

    Path + Digest calculated on the contents of the path



76
77
78
# File 'lib/statica.rb', line 76

def digest_url(url)
  "#{url}/#{digest_for(url)}"
end

#regexRegex

Regex to determine if path is a Static generated path with digest

Returns:

  • (Regex)

    instance



91
92
93
94
# File 'lib/statica.rb', line 91

def regex
  leaders = sub_dirs.collect{|t| Regexp.escape(t)}.join("|")
  @regex ||= /\A\/?(#{leaders})\S*(\/[a-z0-9]+\/?)\Z/
end

#reset_cached_digestHash

Reset cached digest to new hash

Returns:

  • (Hash)

    Current cached_digest initialized to empty hash



39
40
41
# File 'lib/statica.rb', line 39

def reset_cached_digest
  @cached_digest = {}
end

#resolve(path) ⇒ String

If path is a digest_url, remove the digest portion.

Parameters:

  • Static (String)

    resource path

Returns:

  • (String)

    Original path if regex did not match; Path without digest on regex match



84
85
86
# File 'lib/statica.rb', line 84

def resolve(path)
  (path =~ regex) ? path.sub($2, '') : path
end

#sub_dirsArray

List of subdirectories where static resources are held

Returns:

  • (Array)

    Initialized to the default values of: javascripts stylesheets images js css



46
47
48
# File 'lib/statica.rb', line 46

def sub_dirs
  @sub_dirs ||= %w{javascripts stylesheets images js css}
end

#sub_dirs=(array) ⇒ Array

Set the subdirectories if the default values don’t suffice

Parameters:

  • Subdirectories (Array)

    where static assets are stored

Returns:

  • (Array)

    Newly initialized sub_dirs array



54
55
56
# File 'lib/statica.rb', line 54

def sub_dirs=(array)
  @sub_dirs = array
end