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, #get_file_uri, #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

#add_path_prefix(path, path_prefix) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/sinatra/assetpack/package.rb', line 82

def add_path_prefix(path, path_prefix)
  if path_prefix == '/'
    path
  else
    "#{path_prefix}#{path}"
  end
end

#combinedObject



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sinatra/assetpack/package.rb', line 106

def combined
  session = Rack::Test::Session.new(@assets.app)
  paths.map { |path|
    result = session.get(path)
    if result.body.respond_to?(:force_encoding)
      response_encoding = result.content_type.split(/\;\s*charset\s*=\s*/).last.upcase rescue 'ASCII-8BIT'
      result.body.force_encoding(response_encoding).encode(Encoding.default_external || 'ASCII-8BIT')  if result.status == 200
    else
      result.body  if result.status == 200
    end
  }.join("\n")
end

#css?Boolean

Returns:

  • (Boolean)


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

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

#filesObject



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

def files
  paths_and_files.values.compact
end

#hashObject

The cache hash.



98
99
100
101
102
103
104
# File 'lib/sinatra/assetpack/package.rb', line 98

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

#js?Boolean

Returns:

  • (Boolean)


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

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

#minifyObject



90
91
92
93
94
95
# File 'lib/sinatra/assetpack/package.rb', line 90

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)
  list.reject! { |path, file| @assets.ignored?(path) }
  list
end

#production_pathObject

The URI path of the minified file (with cache buster, but not a path prefix)



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

def production_path
  add_cache_buster @path, *files
end

#route_regexObject

Returns the regex for the route, including cache buster.



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

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

#to_development_html(path_prefix, options = {}) ⇒ Object



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

def to_development_html(path_prefix, options={})
  paths_and_files.map { |path, file|
    path = add_cache_buster(path, file)
    path = add_path_prefix(path, path_prefix)
    link_tag(path, options)
  }.join("\n")
end

#to_production_html(path_prefix, options = {}) ⇒ Object



76
77
78
79
80
# File 'lib/sinatra/assetpack/package.rb', line 76

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