Class: Spar::Helpers::Paths

Inherits:
Object
  • Object
show all
Defined in:
lib/spar/helpers.rb

Constant Summary collapse

URI_REGEXP =
%r{^[-a-z]+://|^cid:|^//}

Instance Method Summary collapse

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, options={})
  source = source.to_s
  return nil if is_uri?(source)
  source = rewrite_extension(source, ext)
  Spar.sprockets.find_asset(source, options)
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, options = {})
  source = source.to_s
  return source if is_uri?(source)

  source = rewrite_extension(source, options[:ext]) if options[:ext]
  source = rewrite_asset_path(source, options)
  source = rewrite_host_and_protocol(source, options[: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, options={})
  if Spar.settings['digest'] && !Spar.settings['debug'] && asset = Spar.sprockets.find_asset(logical_path, options)
    return asset.digest_path
  end
  return logical_path
end

#is_uri?(path) ⇒ Boolean

Returns:

  • (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, options = {})
  if source[0] == ?/
    source
  else
    source = digest_for(source, options)
    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

#rewrite_host_and_protocol(source, protocol = nil) ⇒ Object



145
146
147
# File 'lib/spar/helpers.rb', line 145

def rewrite_host_and_protocol(source, protocol = nil)
  Spar.settings['asset_host'] ? "#{Spar.settings['asset_host']}#{source}" : source
end