Class: Sinatra::AssetPack::Package
- Inherits:
-
Object
- Object
- Sinatra::AssetPack::Package
- Includes:
- BusterHelpers, HtmlHelpers
- Defined in:
- lib/sinatra/assetpack/package.rb
Overview
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
Instance Attribute Summary collapse
-
#filespecs ⇒ Object
readonly
Returns the value of attribute filespecs.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #add_path_prefix(path, path_prefix) ⇒ Object
- #combined ⇒ Object
- #css? ⇒ Boolean
- #files ⇒ Object
-
#hash ⇒ Object
The cache hash.
-
#initialize(assets, name, type, path, filespecs) ⇒ Package
constructor
A new instance of Package.
- #js? ⇒ Boolean
- #minify ⇒ Object
- #mtime ⇒ Object
- #paths ⇒ Object
-
#paths_and_files ⇒ Object
Returns a list of URIs.
-
#production_path ⇒ Object
The URI path of the minified file (with cache buster, but not a path prefix).
-
#route_regex ⇒ Object
Returns the regex for the route, including cache buster.
- #to_development_html(path_prefix, options = {}) ⇒ Object
- #to_production_html(path_prefix, options = {}) ⇒ Object
Methods included from BusterHelpers
#add_cache_buster, #cache_buster_hash, #mtime_for
Methods included from HtmlHelpers
Constructor Details
#initialize(assets, name, type, path, filespecs) ⇒ Package
Returns a new instance of Package.
25 26 27 28 29 30 31 |
# 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 |
Instance Attribute Details
#filespecs ⇒ Object (readonly)
Returns the value of attribute filespecs.
35 36 37 |
# File 'lib/sinatra/assetpack/package.rb', line 35 def filespecs @filespecs end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
36 37 38 |
# File 'lib/sinatra/assetpack/package.rb', line 36 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
34 35 36 |
# File 'lib/sinatra/assetpack/package.rb', line 34 def path @path end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
33 34 35 |
# File 'lib/sinatra/assetpack/package.rb', line 33 def type @type end |
Instance Method Details
#add_path_prefix(path, path_prefix) ⇒ Object
82 83 84 85 86 87 88 |
# 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 ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# 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? ⇒ Boolean
120 |
# File 'lib/sinatra/assetpack/package.rb', line 120 def css?() @type == :css; end |
#files ⇒ Object
45 46 47 |
# File 'lib/sinatra/assetpack/package.rb', line 45 def files paths_and_files.values.compact end |
#hash ⇒ Object
The cache hash.
98 99 100 101 102 103 104 |
# File 'lib/sinatra/assetpack/package.rb', line 98 def hash if @assets.app.development? "#{name}.#{type}/#{mtime}" else "#{name}.#{type}" end end |
#js? ⇒ Boolean
119 |
# File 'lib/sinatra/assetpack/package.rb', line 119 def js?() @type == :js; end |
#minify ⇒ Object
90 91 92 93 94 95 |
# File 'lib/sinatra/assetpack/package.rb', line 90 def minify engine = @assets.send(:"#{@type}_compression") = @assets.send(:"#{@type}_compression_options") Compressor.compress combined, @type, engine, end |
#mtime ⇒ Object
53 54 55 |
# File 'lib/sinatra/assetpack/package.rb', line 53 def mtime BusterHelpers.mtime_for(files) end |
#paths ⇒ Object
49 50 51 |
# File 'lib/sinatra/assetpack/package.rb', line 49 def paths paths_and_files.keys end |
#paths_and_files ⇒ Object
Returns a list of URIs
39 40 41 42 43 |
# 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 ⇒ Object
The URI path of the minified file (with cache buster, but not a path prefix)
72 73 74 |
# File 'lib/sinatra/assetpack/package.rb', line 72 def production_path add_cache_buster @path, *files end |
#route_regex ⇒ Object
Returns the regex for the route, including cache buster.
58 59 60 61 |
# 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 = {}) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/sinatra/assetpack/package.rb', line 63 def to_development_html(path_prefix, ={}) paths_and_files.map { |path, file| path = add_cache_buster(path, file) path = add_path_prefix(path, path_prefix) link_tag(path, ) }.join("\n") end |
#to_production_html(path_prefix, options = {}) ⇒ Object
76 77 78 79 80 |
# File 'lib/sinatra/assetpack/package.rb', line 76 def to_production_html(path_prefix, ={}) path = production_path path = add_path_prefix(path, path_prefix) link_tag path, end |