Class: Sinatra::AssetPack::Package

Inherits:
Object
  • Object
show all
Includes:
BusterHelpers, HtmlHelpers
Defined in:
lib/sinatra/assetpack/package.rb

Overview

A package.

Common usage

package = assets.packages['application.css']

package.files   # List of local files
package.paths   # List of URI paths

package.type    # :css or :js
package.css?
package.js?

package.path    # '/css/application.css' => where to serve the compressed file

package.to_development_html
package.to_production_html

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BusterHelpers

#add_cache_buster, #cache_buster_hash, #mtime_for

Methods included from HtmlHelpers

#e, #kv

Constructor Details

#initialize(assets, name, type, path, filespecs) ⇒ Package

Returns a new instance of Package.



25
26
27
28
29
30
31
# File 'lib/sinatra/assetpack/package.rb', line 25

def initialize(assets, name, type, path, filespecs)
  @assets      = assets     # Options instance
  @name        = name       # "application"
  @type        = type       # :js or :css
  @path        = path       # '/js/app.js' -- where to served the compressed file
  @filespecs   = filespecs  # [ '/js/*.js' ]
end

Instance Attribute Details

#filespecsObject (readonly)

Returns the value of attribute filespecs.



35
36
37
# File 'lib/sinatra/assetpack/package.rb', line 35

def filespecs
  @filespecs
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/sinatra/assetpack/package.rb', line 36

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



34
35
36
# File 'lib/sinatra/assetpack/package.rb', line 34

def path
  @path
end

#typeObject (readonly)

Returns the value of attribute type.



33
34
35
# File 'lib/sinatra/assetpack/package.rb', line 33

def type
  @type
end

Instance Method Details

#combinedObject



95
96
97
98
99
100
101
# File 'lib/sinatra/assetpack/package.rb', line 95

def combined
  session = Rack::Test::Session.new(@assets.app)
  paths.map { |path|
    result = session.get(path)
    result.body  if result.status == 200
  }.join("\n")
end

#css?Boolean

Returns:

  • (Boolean)


104
# File 'lib/sinatra/assetpack/package.rb', line 104

def css?() @type == :css; end

#filesObject



45
46
47
# File 'lib/sinatra/assetpack/package.rb', line 45

def files
  paths_and_files.values
end

#hashObject

The cache hash.



87
88
89
90
91
92
93
# File 'lib/sinatra/assetpack/package.rb', line 87

def hash
  if @assets.app.development?
    "#{name}.#{type}/#{mtime}"
  else
    "#{name}.#{type}"
  end
end

#js?Boolean

Returns:

  • (Boolean)


103
# File 'lib/sinatra/assetpack/package.rb', line 103

def js?()  @type == :js; end

#minifyObject



79
80
81
82
83
84
# File 'lib/sinatra/assetpack/package.rb', line 79

def minify
  engine  = @assets.send(:"#{@type}_compression")
  options = @assets.send(:"#{@type}_compression_options")

  Compressor.compress combined, @type, engine, options
end

#mtimeObject



53
54
55
# File 'lib/sinatra/assetpack/package.rb', line 53

def mtime
  BusterHelpers.mtime_for(files)
end

#pathsObject



49
50
51
# File 'lib/sinatra/assetpack/package.rb', line 49

def paths
  paths_and_files.keys
end

#paths_and_filesObject

Returns a list of URIs



39
40
41
42
43
# File 'lib/sinatra/assetpack/package.rb', line 39

def paths_and_files
  list = @assets.glob(@filespecs, :preserve => true)
  list.reject! { |path, file| @assets.ignored?(path) }
  list
end

#production_pathObject

The URI path of the minified file (with cache buster)



71
72
73
# File 'lib/sinatra/assetpack/package.rb', line 71

def production_path
  add_cache_buster @path, *files
end

#route_regexObject

Returns the regex for the route, including cache buster crap.



58
59
60
61
# File 'lib/sinatra/assetpack/package.rb', line 58

def route_regex
  re = @path.gsub(/(.[^.]+)$/) { |ext| "(?:\.[0-9]+)?#{ext}" }
  /^#{re}$/
end

#to_development_html(options = {}) ⇒ Object



63
64
65
66
67
68
# File 'lib/sinatra/assetpack/package.rb', line 63

def to_development_html(options={})
  paths_and_files.map { |path, file|
    path = add_cache_buster(path, file)  # app.css => app.829378.css
    link_tag(path, options)
  }.join("\n")
end

#to_production_html(options = {}) ⇒ Object



75
76
77
# File 'lib/sinatra/assetpack/package.rb', line 75

def to_production_html(options={})
  link_tag production_path, options
end