Module: Jammit

Defined in:
lib/jammit.rb,
lib/jammit/helper.rb,
lib/jammit/routes.rb,
lib/jammit/packager.rb,
lib/jammit/compressor.rb,
lib/jammit/controller.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, DeprecationError, OutputNotWritable, PackageNotFound, Packager

Constant Summary collapse

VERSION =
"0.5.0"
ROOT =
File.expand_path(File.dirname(__FILE__) + '/..')
ASSET_ROOT =
File.expand_path(defined?(Rails) ? Rails.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
JS_EXTENSION =

Extension matchers for JavaScript and JST, which need to be disambiguated.

/\.js\Z/
DEFAULT_JST_EXTENSION =
"jst"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.compress_assetsObject (readonly)

Returns the value of attribute compress_assets.



49
50
51
# File 'lib/jammit.rb', line 49

def compress_assets
  @compress_assets
end

.compressor_optionsObject (readonly)

Returns the value of attribute compressor_options.



49
50
51
# File 'lib/jammit.rb', line 49

def compressor_options
  @compressor_options
end

.configurationObject (readonly)

Returns the value of attribute configuration.



49
50
51
# File 'lib/jammit.rb', line 49

def configuration
  @configuration
end

.css_compressor_optionsObject (readonly)

Returns the value of attribute css_compressor_options.



49
50
51
# File 'lib/jammit.rb', line 49

def css_compressor_options
  @css_compressor_options
end

.embed_assetsObject (readonly)

Returns the value of attribute embed_assets.



49
50
51
# File 'lib/jammit.rb', line 49

def embed_assets
  @embed_assets
end

.gzip_assetsObject (readonly)

Returns the value of attribute gzip_assets.



49
50
51
# File 'lib/jammit.rb', line 49

def gzip_assets
  @gzip_assets
end

.include_jst_scriptObject (readonly)

Returns the value of attribute include_jst_script.



49
50
51
# File 'lib/jammit.rb', line 49

def include_jst_script
  @include_jst_script
end

.javascript_compressorObject (readonly)

Returns the value of attribute javascript_compressor.



49
50
51
# File 'lib/jammit.rb', line 49

def javascript_compressor
  @javascript_compressor
end

.jst_extensionObject (readonly)

Returns the value of attribute jst_extension.



49
50
51
# File 'lib/jammit.rb', line 49

def jst_extension
  @jst_extension
end

.jst_extension_regexpObject (readonly)

Returns the value of attribute jst_extension_regexp.



49
50
51
# File 'lib/jammit.rb', line 49

def jst_extension_regexp
  @jst_extension_regexp
end

.mhtml_enabledObject (readonly)

Returns the value of attribute mhtml_enabled.



49
50
51
# File 'lib/jammit.rb', line 49

def mhtml_enabled
  @mhtml_enabled
end

.package_assetsObject (readonly)

Returns the value of attribute package_assets.



49
50
51
# File 'lib/jammit.rb', line 49

def package_assets
  @package_assets
end

.package_pathObject (readonly)

Returns the value of attribute package_path.



49
50
51
# File 'lib/jammit.rb', line 49

def package_path
  @package_path
end

.template_base_pathObject (readonly)

Returns the value of attribute template_base_path.



49
50
51
# File 'lib/jammit.rb', line 49

def template_base_path
  @template_base_path
end

.template_functionObject (readonly)

Returns the value of attribute template_function.



49
50
51
# File 'lib/jammit.rb', line 49

def template_function
  @template_function
end

.template_namespaceObject (readonly)

Returns the value of attribute template_namespace.



49
50
51
# File 'lib/jammit.rb', line 49

def template_namespace
  @template_namespace
end

Class Method Details

.asset_url(package, extension, suffix = nil, mtime = nil) ⇒ Object

Generates the server-absolute URL to an asset package.



105
106
107
108
# File 'lib/jammit.rb', line 105

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) ⇒ Object

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



99
100
101
102
# File 'lib/jammit.rb', line 99

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

.load_configuration(config_path) ⇒ Object

Load the complete asset configuration from the specified @config_path@.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jammit.rb', line 61

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])
  set_template_extension(conf[:template_extension])
  set_template_base_path(conf[:template_base_path])
  check_java_version
  check_for_deprecations
  self
end

.packagerObject

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



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

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

.reload!Object

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



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

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