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
- #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).
-
#route_regex ⇒ Object
Returns the regex for the route, including cache buster crap.
- #to_development_html(options = {}) ⇒ Object
- #to_production_html(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
#combined ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/sinatra/assetpack/package.rb', line 95 def combined session = Rack::Test::Session.new(@assets.app) paths.map { |path| result = session.get(path) result.body if result.status == 200 }.join("\n") end |
#css? ⇒ Boolean
104 |
# File 'lib/sinatra/assetpack/package.rb', line 104 def css?() @type == :css; end |
#files ⇒ Object
45 46 47 |
# File 'lib/sinatra/assetpack/package.rb', line 45 def files paths_and_files.values end |
#hash ⇒ Object
The cache hash.
87 88 89 90 91 92 93 |
# File 'lib/sinatra/assetpack/package.rb', line 87 def hash if @assets.app.development? "#{name}.#{type}/#{mtime}" else "#{name}.#{type}" end end |
#js? ⇒ Boolean
103 |
# File 'lib/sinatra/assetpack/package.rb', line 103 def js?() @type == :js; end |
#minify ⇒ Object
79 80 81 82 83 84 |
# File 'lib/sinatra/assetpack/package.rb', line 79 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, :preserve => true) list.reject! { |path, file| @assets.ignored?(path) } list end |
#production_path ⇒ Object
The URI path of the minified file (with cache buster)
71 72 73 |
# File 'lib/sinatra/assetpack/package.rb', line 71 def production_path add_cache_buster @path, *files end |
#route_regex ⇒ Object
Returns the regex for the route, including cache buster crap.
58 59 60 61 |
# File 'lib/sinatra/assetpack/package.rb', line 58 def route_regex re = @path.gsub(/(.[^.]+)$/) { |ext| "(?:\.[0-9]+)?#{ext}" } /^#{re}$/ end |
#to_development_html(options = {}) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/sinatra/assetpack/package.rb', line 63 def to_development_html(={}) paths_and_files.map { |path, file| path = add_cache_buster(path, file) # app.css => app.829378.css link_tag(path, ) }.join("\n") end |
#to_production_html(options = {}) ⇒ Object
75 76 77 |
# File 'lib/sinatra/assetpack/package.rb', line 75 def to_production_html(={}) link_tag production_path, end |