Class: JsFiles

Inherits:
Object show all
Defined in:
lib/sinatra/support/compressedjs.rb

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

Metadata methods collapse

Output methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JsFiles

Creates a JsFiles object based on the list of given files.

Examples:


files  = Dir['public/js/jquery.*.js']
$js_files = JsFiles.new(files)

Parameters:

  • files (Array)

    A list of string file paths.



176
177
178
179
180
181
182
183
184
185
# File 'lib/sinatra/support/compressedjs.rb', line 176

def initialize(options={})
  @app    = options[:app]
  @files  = options[:files]
  @prefix = options[:prefix] || '/js/'
  @path   = options[:path] || @prefix + 'app.js'
  @root   = options[:root] || '/app/js'
  @root   = File.expand_path(@root)

  raise "Files must be an array"  unless @files.is_a?(Array)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



163
164
165
# File 'lib/sinatra/support/compressedjs.rb', line 163

def app
  @app
end

#filesObject (readonly)

Returns the value of attribute files.



162
163
164
# File 'lib/sinatra/support/compressedjs.rb', line 162

def files
  @files
end

#pathObject (readonly)

Returns the value of attribute path.



166
167
168
# File 'lib/sinatra/support/compressedjs.rb', line 166

def path
  @path
end

#prefixObject (readonly)

Returns the value of attribute prefix.



164
165
166
# File 'lib/sinatra/support/compressedjs.rb', line 164

def prefix
  @prefix
end

#rootObject (readonly)

Returns the value of attribute root.



165
166
167
# File 'lib/sinatra/support/compressedjs.rb', line 165

def root
  @root
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

#combinedObject

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

#compressedObject

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

#hrefsObject

Returns a list of the URLs for the package.

Examples:


-# This is the same as calling #to_html.
- Main.js_files.hrefs.each do |href|
  %script{:src => href}


205
206
207
208
209
210
211
212
# File 'lib/sinatra/support/compressedjs.rb', line 205

def hrefs
  @files.map { |f|
    path = File.expand_path(f)
    path.gsub! /\.[^\.]*$/, ''
    path.gsub! /^#{@root}/, ''
    File.join @prefix, path + ".js?#{File.mtime(f).to_i}"
  }
end

#mtimeTime

Returns the the modified time of the entire package.

Returns:

  • (Time)

    The last modified time of the most recent file.



193
194
195
# File 'lib/sinatra/support/compressedjs.rb', line 193

def mtime
  @files.map { |f| File.mtime(f) }.max
end

#to_development_htmlObject

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_htmlObject

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_htmlObject

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