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, Controller, PackageNotFound, Packager

Constant Summary

VERSION =
"0.1.0"
ROOT =
File.expand_path(File.dirname(__FILE__) + '/..')
DEFAULT_CONFIG_PATH =
"config/assets.yml"
DEFAULT_PACKAGE_PATH =
"assets"
DEFAULT_JST_SCRIPT =
"#{ROOT}/lib/jammit/jst.js"
DEFAULT_JST_COMPILER =
"template"

Attribute Summary

Method Summary

Attribute Details

+ (Object) configuration (readonly)

Returns the value of attribute configuration



24
25
26
# File 'lib/jammit.rb', line 24

def configuration
  @configuration
end

+ (Object) embed_images (readonly)

Returns the value of attribute embed_images



24
25
26
# File 'lib/jammit.rb', line 24

def embed_images
  @embed_images
end

+ (Object) include_jst_script (readonly)

Returns the value of attribute include_jst_script



24
25
26
# File 'lib/jammit.rb', line 24

def include_jst_script
  @include_jst_script
end

+ (Object) mhtml_enabled (readonly)

Returns the value of attribute mhtml_enabled



24
25
26
# File 'lib/jammit.rb', line 24

def mhtml_enabled
  @mhtml_enabled
end

+ (Object) package_assets (readonly)

Returns the value of attribute package_assets



24
25
26
# File 'lib/jammit.rb', line 24

def package_assets
  @package_assets
end

+ (Object) package_path (readonly)

Returns the value of attribute package_path



24
25
26
# File 'lib/jammit.rb', line 24

def package_path
  @package_path
end

+ (Object) template_function (readonly)

Returns the value of attribute template_function



24
25
26
# File 'lib/jammit.rb', line 24

def template_function
  @template_function
end

Method Details

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

Generates the server-absolute URL to an asset package.



66
67
68
69
# File 'lib/jammit.rb', line 66

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.



60
61
62
63
# File 'lib/jammit.rb', line 60

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.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jammit.rb', line 33

def self.load_configuration(config_path)
  return unless config_path && File.exists?(config_path)
  @config_path        = config_path
  @configuration      = conf = YAML.load_file(@config_path).symbolize_keys
  @package_path       = conf[:package_path] || DEFAULT_PACKAGE_PATH
  @embed_images       = conf[:embed_images]
  @mhtml_enabled      = @embed_images && @embed_images != "datauri"
  set_package_assets(conf[:package_assets])
  set_template_function(conf[:template_function])
  self
end

+ (Object) packager

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



55
56
57
# File 'lib/jammit.rb', line 55

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 before every request to the Jammit::Controller.



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

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

+ (Object) set_package_assets(value) (private)



74
75
76
77
78
79
80
81
82
# File 'lib/jammit.rb', line 74

def self.set_package_assets(value)
  package_env     = !defined?(RAILS_ENV) || RAILS_ENV != 'development'
  @package_assets = case value
  when 'always'     then true
  when false        then false
  when true         then package_env
  when nil          then package_env
  end
end

+ (Object) set_template_function(value) (private)



84
85
86
87
88
89
90
91
92
# File 'lib/jammit.rb', line 84

def self.set_template_function(value)
  @template_function = case value
  when false then ''
  when true  then DEFAULT_JST_COMPILER
  when nil   then DEFAULT_JST_COMPILER
  else            value
  end
  @include_jst_script = @template_function == DEFAULT_JST_COMPILER
end