lib/sprockets/helpers.rb in sprockets-helpers-0.4.0 vs lib/sprockets/helpers.rb in sprockets-helpers-0.5.0
- old
+ new
@@ -1,35 +1,44 @@
-require 'sprockets/helpers/version'
require 'sprockets'
+require 'sprockets/helpers/version'
+require 'sprockets/helpers/base_path'
+require 'sprockets/helpers/asset_path'
+require 'sprockets/helpers/file_path'
+require 'sprockets/helpers/manifest_path'
+require 'uri'
module Sprockets
module Helpers
- autoload :AssetPath, 'sprockets/helpers/asset_path'
- autoload :FilePath, 'sprockets/helpers/file_path'
- autoload :ManifestPath, 'sprockets/helpers/manifest_path'
-
- # Pattern for checking if a given path is an external URI.
- URI_MATCH = %r(^[-a-z]+://|^cid:|^//)
-
class << self
+ # Link to assets from a dedicated server.
+ attr_accessor :asset_host
+
# When true, the asset paths will return digest paths.
attr_accessor :digest
# Set the Sprockets environment to search for assets.
# This defaults to the context's #environment method.
attr_accessor :environment
-
+
# The manifest file used for lookup
attr_accessor :manifest
-
+
# The base URL the Sprocket environment is mapped to.
# This defaults to '/assets'.
def prefix
@prefix ||= '/assets'
end
attr_writer :prefix
+ # Customize the protocol when using asset hosts.
+ # If the value is :relative, A relative protocol ('//')
+ # will be used.
+ def protocol
+ @protocol ||= 'http://'
+ end
+ attr_writer :protocol
+
# The path to the public directory, where the assets
# not managed by Sprockets will be located.
# Defaults to './public'
def public_path
@public_path ||= './public'
@@ -70,29 +79,32 @@
# asset_path '/dir/xmlhr.js', :dir => 'javascripts' # => '/dir/xmlhr.js'
# asset_path 'http://www.example.com/js/xmlhr' # => 'http://www.example.com/js/xmlhr'
# asset_path 'http://www.example.com/js/xmlhr.js' # => 'http://www.example.com/js/xmlhr.js'
#
def asset_path(source, options = {})
- return source if source =~ URI_MATCH
+ uri = URI.parse(source)
+ # Return fast if the URI is absolute
+ return source if uri.absolute?
+
# Append extension if necessary
- if options[:ext] && File.extname(source).empty?
- source << ".#{options[:ext]}"
+ if options[:ext] && File.extname(uri.path).empty?
+ uri.path << ".#{options[:ext]}"
end
-
+
# If a manifest is present, try to grab the path from the manifest first
- if Helpers.manifest && Helpers.manifest.assets[source]
- return ManifestPath.new(Helpers.manifest.assets[source], options).to_s
+ if Helpers.manifest && Helpers.manifest.assets[uri.path]
+ return ManifestPath.new(uri, Helpers.manifest.assets[uri.path], options).to_s
end
-
+
# If the source points to an asset in the Sprockets
# environment use AssetPath to generate the full path.
- assets_environment.resolve(source) do |path|
- return AssetPath.new(assets_environment[path], options).to_s
+ assets_environment.resolve(uri.path) do |path|
+ return AssetPath.new(uri, assets_environment[path], options).to_s
end
# Use FilePath for normal files on the file system
- FilePath.new(source, options).to_s
+ FilePath.new(uri, options).to_s
end
alias_method :path_to_asset, :asset_path
# Computes the path to a javascript asset either in the Sprockets
# environment or the public directory. If the +source+ filename has no extension,