Files

Jammit

@Jammit@ is the central namespace for all Jammit classes, and provides access to all of the configuration options.

Attributes

allow_debugging[R]
compress_assets[R]
compressor_options[R]
compressors[RW]
config_path[R]
configuration[R]
css_compressor_options[R]
embed_assets[R]
gzip_assets[R]
include_jst_script[R]
javascript_compressor[R]
mhtml_enabled[R]
package_assets[R]
package_path[R]
public_root[R]
template_extension[R]
template_extension_matcher[R]
template_function[R]
template_namespace[R]

Public Class Methods

asset_url(package, extension, suffix=nil, mtime=nil) click to toggle source

Generates the server-absolute URL to an asset package.

# File lib/jammit.rb, line 119
def self.asset_url(package, extension, suffix=nil, mtime=nil)
  timestamp = mtime ? "?#{mtime.to_i}" : ''
  "/#{package_path}/#{filename(package, extension, suffix)}#{timestamp}"
end
filename(package, extension, suffix=nil) click to toggle source

Generate the base filename for a version of a given package.

# File lib/jammit.rb, line 113
def self.filename(package, extension, suffix=nil)
  suffix_part  = suffix ? "-#{suffix}" : ''
  "#{package}#{suffix_part}.#{extension}"
end
load_configuration(config_path, soft=false) click to toggle source

Load the complete asset configuration from the specified @config_path@. If we're loading softly, don't let missing configuration error out.

# File lib/jammit.rb, line 67
def self.load_configuration(config_path, soft=false)
  exists = config_path && File.exists?(config_path)
  return false if soft && !exists
  raise MissingConfiguration, "could not find the \"#{config_path}\" configuration file" unless exists
  conf = YAML.load(ERB.new(File.read(config_path)).result)

  # Optionally overwrite configuration based on the environment.
  rails_env = defined?(Rails) ? Rails.env : ENV['RAILS_ENV']
  conf.merge! conf.delete rails_env if conf.has_key? rails_env

  @config_path            = config_path
  @configuration          = symbolize_keys(conf)
  @package_path           = conf[:package_path] || DEFAULT_PACKAGE_PATH
  @embed_assets           = conf[:embed_assets] || conf[:embed_images]
  @compress_assets        = !(conf[:compress_assets] == false)
  @gzip_assets            = !(conf[:gzip_assets] == false)
  @allow_debugging        = !(conf[:allow_debugging] == false)
  @mhtml_enabled          = @embed_assets && @embed_assets != "datauri"
  @compressor_options     = symbolize_keys(conf[:compressor_options] || {})
  @css_compressor_options = symbolize_keys(conf[:css_compressor_options] || {})
  set_javascript_compressor(conf[:javascript_compressor])
  set_package_assets(conf[:package_assets])
  set_template_function(conf[:template_function])
  set_template_namespace(conf[:template_namespace])
  set_template_extension(conf[:template_extension])
  set_public_root(conf[:public_root]) if conf[:public_root]
  symbolize_keys(conf[:stylesheets]) if conf[:stylesheets]
  symbolize_keys(conf[:javascripts]) if conf[:javascripts]
  check_for_deprecations
  self
end
package!(options={}) click to toggle source

Convenience method for packaging up Jammit, using the default options.

# File lib/jammit.rb, line 125
def self.package!(options={})
  options = {
    :config_path    => Jammit::DEFAULT_CONFIG_PATH,
    :output_folder  => nil,
    :base_url       => nil,
    :public_root    => nil,
    :force          => false
  }.merge(options)
  load_configuration(options[:config_path])
  set_public_root(options[:public_root]) if options[:public_root]
  packager.force         = options[:force]
  packager.package_names = options[:package_names]
  packager.precache_all(options[:output_folder], options[:base_url])
end
packager() click to toggle source

Keep a global (thread-local) reference to a @Jammit::Packager@, to avoid recomputing asset lists unnecessarily.

# File lib/jammit.rb, line 108
def self.packager
  Thread.current[:jammit_packager] ||= Packager.new
end
reload!() click to toggle source

Force a reload by resetting the Packager and reloading the configuration. In development, this will be called as a before_filter before every request.

# File lib/jammit.rb, line 101
def self.reload!
  Thread.current[:jammit_packager] = nil
  load_configuration(@config_path)
end

Private Class Methods

check_for_deprecations() click to toggle source

Jammit 0.5+ no longer supports separate template packages.

# File lib/jammit.rb, line 199
def self.check_for_deprecations
  if @configuration[:templates]
    raise DeprecationError, "Jammit 0.5+ no longer supports separate packages for templates.\nPlease fold your templates into the appropriate 'javascripts' package instead."
  end
end
check_java_version() click to toggle source

The YUI Compressor requires Java > 1.4, and Closure requires Java > 1.6.

# File lib/jammit.rb, line 180
def self.check_java_version
  return true if @checked_java_version
  java = @compressor_options[:java] || 'java'
  @css_compressor_options[:java] ||= java if @compressor_options[:java]
  version = (`#{java} -version 2>&1`)[/\d+\.\d+/]
  disable_compression if !version ||
    (@javascript_compressor == :closure && version < '1.6') ||
    (@javascript_compressor == :yui && version < '1.4')
  @checked_java_version = true
end
disable_compression() click to toggle source

If we don't have a working Java VM, then disable asset compression and complain loudly.

# File lib/jammit.rb, line 193
def self.disable_compression
  @compress_assets = false
  warn("Asset compression disabled -- Java unavailable.")
end
set_javascript_compressor(value) click to toggle source

Ensure that the JavaScript compressor is a valid choice.

# File lib/jammit.rb, line 149
def self.set_javascript_compressor(value)
  value = value && value.to_sym
  @javascript_compressor = compressors.include?(value) ? value : DEFAULT_COMPRESSOR
end
set_package_assets(value) click to toggle source

Turn asset packaging on or off, depending on configuration and environment.

# File lib/jammit.rb, line 155
def self.set_package_assets(value)
  package_env     = !defined?(Rails) || (!Rails.env.development? && !Rails.env.test?)
  @package_assets = value == true || value.nil? ? package_env :
                    value == 'always'           ? true : false
end
set_public_root(public_root=nil) click to toggle source

Allows command-line definition of `PUBLIC_ROOT`, for those using Jammit outside of Rails.

# File lib/jammit.rb, line 144
def self.set_public_root(public_root=nil)
  @public_root = public_root if public_root
end
set_template_extension(value) click to toggle source

Set the extension for JS templates.

# File lib/jammit.rb, line 174
def self.set_template_extension(value)
  @template_extension = (value == true || value.nil? ? DEFAULT_JST_EXTENSION : value.to_s).gsub(/\A\.?(.*)\Z/, '\1')
  @template_extension_matcher = /\.#{Regexp.escape(@template_extension)}\Z/
end
set_template_function(value) click to toggle source

Assign the JST template function, unless explicitly turned off.

# File lib/jammit.rb, line 162
def self.set_template_function(value)
  @template_function = value == true || value.nil? ? DEFAULT_JST_COMPILER :
                       value == false              ? '' : value
  @include_jst_script = @template_function == DEFAULT_JST_COMPILER
end
set_template_namespace(value) click to toggle source

Set the root JS object in which to stash all compiled JST.

# File lib/jammit.rb, line 169
def self.set_template_namespace(value)
  @template_namespace = value == true || value.nil? ? DEFAULT_JST_NAMESPACE : value.to_s
end
symbolize_keys(hash) click to toggle source

Clone of active_support's symbolize_keys, so that we don't have to depend on active_support in any fashion. Converts a hash's keys to all symbols.

# File lib/jammit.rb, line 212
def self.symbolize_keys(hash)
  hash.keys.each do |key|
    hash[(key.to_sym rescue key) || key] = hash.delete(key)
  end
  hash
end
warn(message) click to toggle source
# File lib/jammit.rb, line 205
def self.warn(message)
  message = "Jammit Warning: #{message}"
  $stderr.puts message
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.