Class: Sinatra::Sprockets::Helpers::AssetPaths
- Inherits:
-
Object
- Object
- Sinatra::Sprockets::Helpers::AssetPaths
show all
- Defined in:
- lib/sinatra/sprockets/asset_paths.rb
Defined Under Namespace
Classes: AssetNotPrecompiledError
Instance Method Summary
collapse
Constructor Details
#initialize(config) ⇒ AssetPaths
Returns a new instance of AssetPaths.
6
7
8
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 6
def initialize config
@config = config
end
|
Instance Method Details
#arity_of(callable) ⇒ Object
119
120
121
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 119
def arity_of(callable)
callable.respond_to?(:arity) ? callable.arity : callable.method(:call).arity
end
|
#asset_for(source, ext) ⇒ Object
10
11
12
13
14
15
16
17
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 10
def asset_for(source, ext)
source = source.to_s
return nil if is_uri?(source)
source = rewrite_extension(source, nil, ext)
asset_environment[source]
rescue Sprockets::FileOutsidePaths
nil
end
|
#compute_asset_host(source) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 84
def compute_asset_host(source)
if host = @config.host
if host.respond_to?(:call)
args = [source]
arity = arity_of(host)
if arity > 1 && request.nil?
invalid_asset_host!("Remove the second argument to your asset_host Proc if you do not need the request.")
end
args << current_request if (arity > 1 || arity < 0) && has_request?
host.call(*args)
else
(host =~ /%d/) ? host % (Zlib.crc32(source) % 4) : host
end
end
end
|
#compute_protocol(protocol) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 104
def compute_protocol(protocol)
protocol ||= default_protocol
case protocol
when :request
if request.nil?
invalid_asset_host!("The protocol requested was :request. Consider using :relative instead.")
end
request.protocol
when :relative
"//"
else
"#{protocol}://"
end
end
|
#compute_public_path(source, dir, options = {}) ⇒ Object
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 53
def compute_public_path(source, dir, options = {})
source = source.to_s
unless is_uri?(source)
source = rewrite_extension(source, dir, options[:ext]) if options[:ext]
source = rewrite_asset_path(source, dir, options)
source = rewrite_relative_url_root(source, @config.relative_url_root)
source = rewrite_host_and_protocol(source, options[:protocol])
end
source
end
|
#default_protocol ⇒ Object
100
101
102
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 100
def default_protocol
@config.default_protocol || (request.nil?? :relative : :request)
end
|
#digest_for(logical_path) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 19
def digest_for(logical_path)
if @config.digest && @config.digests && (digest = @config.digests[logical_path])
return digest
end
if @config.compile
if @config.digest && asset = asset_environment[logical_path]
return asset.digest_path
end
return logical_path
else
raise AssetNotPrecompiledError.new("#{logical_path} isn't precompiled")
end
end
|
#invalid_asset_host!(help_message) ⇒ Object
123
124
125
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 123
def invalid_asset_host!(help_message)
raise ActionController::RoutingError, "This asset host cannot be computed without a request in scope. #{help_message}"
end
|
#is_uri?(path) ⇒ Boolean
64
65
66
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 64
def is_uri?(path)
path =~ %r{^[-a-z]+://|^cid:|^//}
end
|
#rewrite_asset_path(source, dir, options = {}) ⇒ Object
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 34
def rewrite_asset_path(source, dir, options = {})
if source[0] == ?/
source
else
source = digest_for(source) unless options[:digest] == false
source = File.join(dir, source)
source = "/#{source}" unless source =~ /^\//
source
end
end
|
#rewrite_extension(source, dir, ext) ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 45
def rewrite_extension(source, dir, ext)
if ext && File.extname(source) != ".#{ext}"
"#{source}.#{ext}"
else
source
end
end
|
#rewrite_host_and_protocol(source, protocol = nil) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 68
def rewrite_host_and_protocol(source, protocol = nil)
host = compute_asset_host(source)
if host && !is_uri?(host)
if (protocol || default_protocol) == :request && !has_request?
host = nil
else
host = "#{compute_protocol(protocol)}#{host}"
end
end
host ? "#{host}#{source}" : source
end
|
#rewrite_relative_url_root(source, relative_url_root) ⇒ Object
80
81
82
|
# File 'lib/sinatra/sprockets/asset_paths.rb', line 80
def rewrite_relative_url_root(source, relative_url_root)
relative_url_root && !source.starts_with?("#{relative_url_root}/") ? "#{relative_url_root}#{source}" : source
end
|