Class: Sinatra::Sprockets::AssetPaths

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/sprockets/asset_paths.rb

Defined Under Namespace

Classes: AssetNotPrecompiledError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AssetPaths

Returns a new instance of AssetPaths.



8
9
10
# File 'lib/sinatra/sprockets/asset_paths.rb', line 8

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/sinatra/sprockets/asset_paths.rb', line 4

def config
  @config
end

Instance Method Details

#arity_of(callable) ⇒ Object



122
123
124
# File 'lib/sinatra/sprockets/asset_paths.rb', line 122

def arity_of(callable)
  callable.respond_to?(:arity) ? callable.arity : callable.method(:call).arity
end

#asset_for(source, ext) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/sinatra/sprockets/asset_paths.rb', line 12

def asset_for(source, ext)
  source = source.to_s
  return nil if is_uri?(source)
  source = rewrite_extension(source, nil, ext)
  config.environment[source]
rescue ::Sprockets::FileOutsidePaths
  nil
end

#compute_asset_host(source) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sinatra/sprockets/asset_paths.rb', line 87

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sinatra/sprockets/asset_paths.rb', line 107

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



37
38
39
40
41
42
43
44
45
46
# File 'lib/sinatra/sprockets/asset_paths.rb', line 37

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_protocolObject



103
104
105
# File 'lib/sinatra/sprockets/asset_paths.rb', line 103

def default_protocol
  config.default_protocol || (request.nil?? :relative : :request)
end

#digest_for(logical_path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sinatra/sprockets/asset_paths.rb', line 21

def digest_for(logical_path)
  if config.digest_assets? && config.digests && (digest = config.digests[logical_path])
    digest
  else
    if config.compile_assets?
      if config.digest_assets? && asset = config.environment[logical_path]
        asset.digest_path
      else
        logical_path
      end
    else
      raise AssetNotPrecompiledError.new("#{logical_path} isn't precompiled")
    end
  end
end

#invalid_asset_host!(help_message) ⇒ Object

Raises:

  • (ActionController::RoutingError)


126
127
128
# File 'lib/sinatra/sprockets/asset_paths.rb', line 126

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

Returns:

  • (Boolean)


48
49
50
# File 'lib/sinatra/sprockets/asset_paths.rb', line 48

def is_uri?(path)
  path =~ %r{^[-a-z]+://|^cid:|^//}
end

#rewrite_asset_path(source, dir, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/sinatra/sprockets/asset_paths.rb', line 68

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



79
80
81
82
83
84
85
# File 'lib/sinatra/sprockets/asset_paths.rb', line 79

def rewrite_extension(source, dir, ext)
  if ext && File.extname(source).empty?
    "#{source}.#{ext}"
  else
    source
  end
end

#rewrite_host_and_protocol(source, protocol = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sinatra/sprockets/asset_paths.rb', line 52

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



64
65
66
# File 'lib/sinatra/sprockets/asset_paths.rb', line 64

def rewrite_relative_url_root(source, relative_url_root)
  relative_url_root && !source.starts_with?("#{relative_url_root}/") ? "#{relative_url_root}#{source}" : source
end