Module: Statica
Defined Under Namespace
Classes: ResourceNotFoundError
Constant Summary collapse
- VERSION =
Statica version string
'0.3.0'
Instance Attribute Summary collapse
-
#root_dir ⇒ Object
Fully qualified path to the root directory of the static assets.
Instance Method Summary collapse
-
#cached_digest ⇒ Hash
Current cache.
-
#digest ⇒ Digest::Base
Current digest.
-
#digest_class=(klass) ⇒ Object
Set digest class.
-
#digest_for(path) ⇒ String
Calculate the hex digest for the given resource.
-
#digest_url(url) ⇒ String
Append digest to url.
-
#regex ⇒ Regex
Regex to determine if path is a Static generated path with digest.
-
#reset_cached_digest ⇒ Hash
Reset cached digest to new hash.
-
#resolve(path) ⇒ String
If path is a digest_url, remove the digest portion.
-
#sub_dirs ⇒ Array
List of subdirectories where static resources are held.
-
#sub_dirs=(array) ⇒ Array
Set the subdirectories if the default values don’t suffice.
Instance Attribute Details
#root_dir ⇒ Object
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_digest ⇒ Hash
Current cache
32 33 34 |
# File 'lib/statica.rb', line 32 def cached_digest @cached_digest ||= {} end |
#digest ⇒ Digest::Base
Current digest
25 26 27 |
# File 'lib/statica.rb', line 25 def digest (@digest || ::Digest::SHA256).new end |
#digest_class=(klass) ⇒ Object
Set digest class
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
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
76 77 78 |
# File 'lib/statica.rb', line 76 def digest_url(url) "#{url}/#{digest_for(url)}" end |
#regex ⇒ Regex
Regex to determine if path is a Static generated path with digest
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_digest ⇒ Hash
Reset cached digest to new 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.
84 85 86 |
# File 'lib/statica.rb', line 84 def resolve(path) (path =~ regex) ? path.sub($2, '') : path end |
#sub_dirs ⇒ Array
List of subdirectories where static resources are held
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
54 55 56 |
# File 'lib/statica.rb', line 54 def sub_dirs=(array) @sub_dirs = array end |