Module: Jammit

Defined in:
lib/jammit.rb,
lib/jammit/routes.rb,
lib/jammit/helper.rb,
lib/jammit/packager.rb,
lib/jammit/controller.rb,
lib/jammit/compressor.rb,
lib/jammit/command_line.rb

Overview

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

Defined Under Namespace

Modules: Helper, Routes Classes: CommandLine, Compressor, ConfigurationNotFound, Controller, OutputNotWritable, PackageNotFound, Packager

Constant Summary

VERSION =
"0.4.4"
ROOT =
File.expand_path(File.dirname(__FILE__) + '/..')
ASSET_ROOT =
File.expand_path(defined?(Rails) ? Rails.root : ".") unless defined?(ASSET_ROOT)
PUBLIC_ROOT =
defined?(Rails) ? Rails.public_path : File.join(ASSET_ROOT, 'public')
DEFAULT_CONFIG_PATH =
File.join(ASSET_ROOT, 'config', 'assets.yml')
DEFAULT_PACKAGE_PATH =
"assets"
DEFAULT_JST_SCRIPT =
File.join(ROOT, 'lib/jammit/jst.js')
DEFAULT_JST_COMPILER =
"template"
DEFAULT_JST_NAMESPACE =
"window.JST"
AVAILABLE_COMPRESSORS =
[:yui, :closure]
DEFAULT_COMPRESSOR =
:yui

Attribute Summary

Method Summary

Attribute Details

+ (Object) compress_assets (readonly)

Returns the value of attribute compress_assets



42
43
44
# File 'lib/jammit.rb', line 42

def compress_assets
  @compress_assets
end

+ (Object) compressor_options (readonly)

Returns the value of attribute compressor_options



42
43
44
# File 'lib/jammit.rb', line 42

def compressor_options
  @compressor_options
end

+ (Object) configuration (readonly)

Returns the value of attribute configuration



42
43
44
# File 'lib/jammit.rb', line 42

def configuration
  @configuration
end

+ (Object) css_compressor_options (readonly)

Returns the value of attribute css_compressor_options



42
43
44
# File 'lib/jammit.rb', line 42

def css_compressor_options
  @css_compressor_options
end

+ (Object) embed_assets (readonly)

Returns the value of attribute embed_assets



42
43
44
# File 'lib/jammit.rb', line 42

def embed_assets
  @embed_assets
end

+ (Object) gzip_assets (readonly)

Returns the value of attribute gzip_assets



42
43
44
# File 'lib/jammit.rb', line 42

def gzip_assets
  @gzip_assets
end

+ (Object) include_jst_script (readonly)

Returns the value of attribute include_jst_script



42
43
44
# File 'lib/jammit.rb', line 42

def include_jst_script
  @include_jst_script
end

+ (Object) javascript_compressor (readonly)

Returns the value of attribute javascript_compressor



42
43
44
# File 'lib/jammit.rb', line 42

def javascript_compressor
  @javascript_compressor
end

+ (Object) mhtml_enabled (readonly)

Returns the value of attribute mhtml_enabled



42
43
44
# File 'lib/jammit.rb', line 42

def mhtml_enabled
  @mhtml_enabled
end

+ (Object) package_assets (readonly)

Returns the value of attribute package_assets



42
43
44
# File 'lib/jammit.rb', line 42

def package_assets
  @package_assets
end

+ (Object) package_path (readonly)

Returns the value of attribute package_path



42
43
44
# File 'lib/jammit.rb', line 42

def package_path
  @package_path
end

+ (Object) template_function (readonly)

Returns the value of attribute template_function



42
43
44
# File 'lib/jammit.rb', line 42

def template_function
  @template_function
end

+ (Object) template_namespace (readonly)

Returns the value of attribute template_namespace



42
43
44
# File 'lib/jammit.rb', line 42

def template_namespace
  @template_namespace
end

Method Details

+ (Object) asset_url(package, extension, suffix = nil, mtime = nil)

Generates the server-absolute URL to an asset package.



94
95
96
97
# File 'lib/jammit.rb', line 94

def self.asset_url(package, extension, suffix=nil, mtime=nil)
  timestamp = mtime ? "?#{mtime.to_i}" : ''
  "/#{package_path}/#{filename(package, extension, suffix)}#{timestamp}"
end

+ (Object) filename(package, extension, suffix = nil)

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



88
89
90
91
# File 'lib/jammit.rb', line 88

def self.filename(package, extension, suffix=nil)
  suffix_part  = suffix ? "-#{suffix}" : ''
  "#{package}#{suffix_part}.#{extension}"
end

+ (Object) load_configuration(config_path)

Load the complete asset configuration from the specified config_path.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jammit.rb', line 53

def self.load_configuration(config_path)
  exists = config_path && File.exists?(config_path)
  raise ConfigurationNotFound, "could not find the \"#{config_path}\" configuration file" unless exists
  conf = YAML.load(ERB.new(File.read(config_path)).result)
  @config_path            = config_path
  @configuration          = conf = conf.symbolize_keys
  @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)
  @mhtml_enabled          = @embed_assets && @embed_assets != "datauri"
  @compressor_options     = (conf[:compressor_options] || {}).symbolize_keys
  @css_compressor_options = (conf[:css_compressor_options] || {}).symbolize_keys
  set_javascript_compressor(conf[:javascript_compressor])
  set_package_assets(conf[:package_assets])
  set_template_function(conf[:template_function])
  set_template_namespace(conf[:template_namespace])
  check_java_version
  self
end

+ (Object) packager

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



83
84
85
# File 'lib/jammit.rb', line 83

def self.packager
  Thread.current[:jammit_packager] ||= Packager.new
end

+ (Object) reload!

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



76
77
78
79
# File 'lib/jammit.rb', line 76

def self.reload!
  Thread.current[:jammit_packager] = nil
  load_configuration(@config_path)
end

+ (Object) check_java_version (private)

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



128
129
130
131
132
133
134
135
# File 'lib/jammit.rb', line 128

def self.check_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')
end

+ (Object) disable_compression (private)

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



139
140
141
142
# File 'lib/jammit.rb', line 139

def self.disable_compression
  @compress_assets = false
  warn("Asset compression disabled -- Java unavailable.")
end

+ (Object) set_javascript_compressor(value) (private)

Ensure that the JavaScript compressor is a valid choice.



103
104
105
106
# File 'lib/jammit.rb', line 103

def self.set_javascript_compressor(value)
  value = value && value.to_sym
  @javascript_compressor = AVAILABLE_COMPRESSORS.include?(value) ? value : DEFAULT_COMPRESSOR
end

+ (Object) set_package_assets(value) (private)

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



109
110
111
112
113
# File 'lib/jammit.rb', line 109

def self.set_package_assets(value)
  package_env     = !defined?(Rails) || !Rails.env.development?
  @package_assets = value == true || value.nil? ? package_env :
                    value == 'always'           ? true : false
end

+ (Object) set_template_function(value) (private)

Assign the JST template function, unless explicitly turned off.



116
117
118
119
120
# File 'lib/jammit.rb', line 116

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

+ (Object) set_template_namespace(value) (private)

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



123
124
125
# File 'lib/jammit.rb', line 123

def self.set_template_namespace(value)
  @template_namespace = value == true || value.nil? ? DEFAULT_JST_NAMESPACE : value.to_s
end

+ (Object) warn(message) (private)



144
145
146
147
148
149
# File 'lib/jammit.rb', line 144

def self.warn(message)
  message = "Jammit Warning: #{message}"
  @logger ||= (defined?(Rails) && Rails.logger ? Rails.logger :
               defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER : nil)
  @logger ? @logger.warn(message) : STDERR.puts(message)
end