Class: JsFiles
Overview
A list of JavaScript files.
This is a general-purpose class usually used for minification of JS assets.
This is often used with Sinatra, but can work with any other web framework.
Usage example
In Sinatra, doing CompressedJS#serve_compressed_js will make a JsFiles instance:
serve_compressed_js :js_files,
:prefix => '/javascript',
:path => '/javascript/combined.js',
:root => './app/js'
files =>
Dir['public/js/jquery.*.js'].sort +
Dir['public/js/app.*.js'].sort
js_files.is_a?(JsFiles) #=> true
js_files.mtime #=> (Time) 2010-09-02 8:00PM
Or outside Sinatra, just instanciate it as so:
js_files = JsFiles.new(:files => files,
:prefix => '/javascript',
:root => './app/js')
You can use #to_html in views:
<%= js_files.to_html %>
Getting the data (for rake tasks perhaps):
File.open('public/scripts.js', 'w') do |f|
f << js_files.combined
end
File.open('public/scripts.min.js', 'w') do |f|
f << js_files.compressed
end
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Metadata methods collapse
-
#hrefs ⇒ Object
Returns a list of the URLs for the package.
-
#mtime ⇒ Time
Returns the the modified time of the entire package.
-
#to_development_html ⇒ Object
Returns the
Creates a JsFiles object based on the list of given files.
176 177 178 179 180 181 182 183 184 185
# File 'lib/sinatra/support/compressedjs.rb', line 176 def initialize(={}) @app = [:app] @files = [:files] @prefix = [:prefix] || '/js/' @path = [:path] || @prefix + 'app.js' @root = [:root] || '/app/js' @root = File.(@root) raise "Files must be an array" unless @files.is_a?(Array) end