class Sinatra::AssetPack::Package
A package.
Common usage¶ ↑
package = assets.packages['application.css'] package.files # List of local files package.paths # List of URI paths package.type # :css or :js package.css? package.js? package.path # '/css/application.css' => where to serve the compressed file package.to_development_html package.to_production_html
Attributes
filespecs[R]
name[R]
path[R]
type[R]
Public Class Methods
new(assets, name, type, path, filespecs)
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 25 def initialize(assets, name, type, path, filespecs) @assets = assets # Options instance @name = name # "application" @type = type # :js or :css @path = path # '/js/app.js' -- where to served the compressed file @filespecs = filespecs # [ '/js/*.js' ] end
Public Instance Methods
add_path_prefix(path, path_prefix)
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 82 def add_path_prefix(path, path_prefix) if path_prefix == '/' path else "#{path_prefix}#{path}" end end
combined()
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 106 def combined session = Rack::Test::Session.new(@assets.app) paths.map { |path| result = session.get(path) if result.body.respond_to?(:force_encoding) response_encoding = result.content_type.split(/\;\s*charset\s*=\s*/).last.upcase rescue 'ASCII-8BIT' result.body.force_encoding(response_encoding).encode(Encoding.default_external || 'ASCII-8BIT') if result.status == 200 else result.body if result.status == 200 end }.join("\n") end
css?()
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 120 def css?() @type == :css; end
files()
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 45 def files paths_and_files.values.compact end
hash()
click to toggle source
The cache hash.
# File lib/sinatra/assetpack/package.rb, line 98 def hash if @assets.app.development? "#{name}.#{type}/#{mtime}" else "#{name}.#{type}" end end
js?()
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 119 def js?() @type == :js; end
minify()
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 90 def minify engine = @assets.send(:"#{@type}_compression") options = @assets.send(:"#{@type}_compression_options") Compressor.compress combined, @type, engine, options end
mtime()
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 53 def mtime BusterHelpers.mtime_for(files) end
paths()
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 49 def paths paths_and_files.keys end
paths_and_files()
click to toggle source
Returns a list of URIs
# File lib/sinatra/assetpack/package.rb, line 39 def paths_and_files list = @assets.glob(@filespecs) list.reject! { |path, file| @assets.ignored?(path) } list end
production_path()
click to toggle source
The URI path of the minified file (with cache buster, but not a path prefix)
# File lib/sinatra/assetpack/package.rb, line 72 def production_path add_cache_buster @path, *files end
route_regex()
click to toggle source
Returns the regex for the route, including cache buster.
# File lib/sinatra/assetpack/package.rb, line 58 def route_regex re = @path.gsub(/(.[^.]+)$/) { |ext| "(?:\.[a-f0-9]{32})?#{ext}" } /^#{re}$/ end
to_development_html(path_prefix, options={})
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 63 def to_development_html(path_prefix, options={}) paths_and_files.map { |path, file| path = add_cache_buster(path, file) path = add_path_prefix(path, path_prefix) link_tag(path, options) }.join("\n") end
to_production_html(path_prefix, options={})
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 76 def to_production_html(path_prefix, options={}) path = production_path path = add_path_prefix(path, path_prefix) link_tag path, options end
Private Instance Methods
link_tag(file, options={})
click to toggle source
# File lib/sinatra/assetpack/package.rb, line 123 def link_tag(file, options={}) file_path = HtmlHelpers.get_file_uri(file, @assets) if js? "<script src='#{e(file_path)}'#{kv(options)}></script>" elsif css? "<link rel='stylesheet' href='#{e(file_path)}'#{kv(options)} />" end end