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:
<!-- Shows <script> tags -->
<%= 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 <script> tags for the development version.
-
#to_html ⇒ Object
Returns the <script> tags, using development or production as needed.
-
#to_production_html ⇒ Object
Returns the <script> tag for the production version.
Output methods collapse
- .compress(str) ⇒ Object
-
#combined ⇒ Object
Returns the combined source of all the files.
-
#compressed ⇒ Object
Returns a combined, minifed source of all the files.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ JsFiles
constructor
Creates a JsFiles object based on the list of given files.
Constructor Details
#initialize(options = {}) ⇒ JsFiles
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 |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
163 164 165 |
# File 'lib/sinatra/support/compressedjs.rb', line 163 def app @app end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
162 163 164 |
# File 'lib/sinatra/support/compressedjs.rb', line 162 def files @files end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
166 167 168 |
# File 'lib/sinatra/support/compressedjs.rb', line 166 def path @path end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
164 165 166 |
# File 'lib/sinatra/support/compressedjs.rb', line 164 def prefix @prefix end |
Class Method Details
.compress(str) ⇒ Object
246 247 248 |
# File 'lib/sinatra/support/compressedjs.rb', line 246 def self.compress(str) JSMin.minify(str).strip end |
Instance Method Details
#combined ⇒ Object
Returns the combined source of all the files.
232 233 234 235 236 237 238 |
# File 'lib/sinatra/support/compressedjs.rb', line 232 def combined @combined ||= @files.map { |file| contents = File.open(file) { |f| f.read } contents = coffee_compile(contents) if file =~ /\.coffee$/ contents }.join("\n") end |
#compressed ⇒ Object
Returns a combined, minifed source of all the files.
241 242 243 244 |
# File 'lib/sinatra/support/compressedjs.rb', line 241 def compressed require 'jsmin' @compressed ||= self.class.compress(combined) end |
#hrefs ⇒ Object
Returns a list of the URLs for the package.
205 206 207 208 209 210 211 212 |
# File 'lib/sinatra/support/compressedjs.rb', line 205 def hrefs @files.map { |f| path = File.(f) path.gsub! /\.[^\.]*$/, '' path.gsub! /^#{@root}/, '' File.join @prefix, path + ".js?#{File.mtime(f).to_i}" } end |
#mtime ⇒ Time
Returns the the modified time of the entire package.
193 194 195 |
# File 'lib/sinatra/support/compressedjs.rb', line 193 def mtime @files.map { |f| File.mtime(f) }.max end |
#to_development_html ⇒ Object
Returns the <script> tags for the development version.
215 216 217 |
# File 'lib/sinatra/support/compressedjs.rb', line 215 def to_development_html hrefs.map { |href| "<script type='text/javascript' src='#{href}'></script>" }.join("\n") end |
#to_html ⇒ Object
Returns the <script> tags, using development or production as needed.
225 226 227 |
# File 'lib/sinatra/support/compressedjs.rb', line 225 def to_html production? ? to_production_html : to_development_html end |
#to_production_html ⇒ Object
Returns the <script> tag for the production version.
220 221 222 |
# File 'lib/sinatra/support/compressedjs.rb', line 220 def to_production_html "<script type='text/javascript' src='%s?%s'></script>" % [path, mtime.to_i] end |