Class: Spar::Helpers::Paths
- Inherits:
-
Object
- Object
- Spar::Helpers::Paths
- Defined in:
- lib/spar/helpers.rb
Constant Summary collapse
- URI_REGEXP =
%r{^[-a-z]+://|^cid:|^//}
Instance Method Summary collapse
- #asset_for(source, ext, options = {}) ⇒ Object
- #compute_public_path(source, options = {}) ⇒ Object
- #digest_for(logical_path, options = {}) ⇒ Object
- #is_uri?(path) ⇒ Boolean
- #rewrite_asset_path(source, options = {}) ⇒ Object
- #rewrite_extension(source, ext) ⇒ Object
- #rewrite_host_and_protocol(source, protocol = nil) ⇒ Object
Instance Method Details
#asset_for(source, ext, options = {}) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/spar/helpers.rb', line 97 def asset_for(source, ext, ={}) source = source.to_s return nil if is_uri?(source) source = rewrite_extension(source, ext) Spar.sprockets.find_asset(source, ) rescue Sprockets::FileOutsidePaths nil end |
#compute_public_path(source, options = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/spar/helpers.rb', line 106 def compute_public_path(source, = {}) source = source.to_s return source if is_uri?(source) source = rewrite_extension(source, [:ext]) if [:ext] source = rewrite_asset_path(source, ) source = rewrite_host_and_protocol(source, [:protocol]) source end |
#digest_for(logical_path, options = {}) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/spar/helpers.rb', line 120 def digest_for(logical_path, ={}) if Spar.settings['digest'] && !Spar.settings['debug'] && asset = Spar.sprockets.find_asset(logical_path, ) return asset.digest_path end return logical_path end |
#is_uri?(path) ⇒ Boolean
116 117 118 |
# File 'lib/spar/helpers.rb', line 116 def is_uri?(path) path =~ URI_REGEXP end |
#rewrite_asset_path(source, options = {}) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/spar/helpers.rb', line 127 def rewrite_asset_path(source, = {}) if source[0] == ?/ source else source = digest_for(source, ) source = "/#{source}" unless source =~ /^\// source end end |
#rewrite_extension(source, ext) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/spar/helpers.rb', line 137 def rewrite_extension(source, ext) if ext && File.extname(source) != ".#{ext}" "#{source}.#{ext}" else source end end |