Class: Pinion::Bundle
Overview
A ‘Bundle` is a set of assets of the same type that will be served as a single grouped asset in production. A `Bundle` has a `BundleType` that defines how to process the bundle.
Constant Summary collapse
- @@bundles =
Each bundle is cached by name.
{}
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Attributes inherited from Asset
#checksum, #extension, #length, #mtime
Class Method Summary collapse
-
.[](name) ⇒ Object
Find a ‘Bundle` by its name.
-
.create(name, bundle_type_name, paths) ⇒ Object
Create a new bundle from a bundle_type name (e.g. ‘:concatenate_and_uglify_js`) and an array of paths.
Instance Method Summary collapse
-
#initialize(bundle_type, name, paths) ⇒ Bundle
constructor
Create a new ‘Bundle`.
Methods inherited from Asset
#content_type, #each, find_file, find_source_file_and_conversion, find_uncached_asset, #invalidate, #latest_mtime, static, watch_path
Constructor Details
#initialize(bundle_type, name, paths) ⇒ Bundle
Create a new ‘Bundle`.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pinion/bundle.rb', line 17 def initialize(bundle_type, name, paths) @name = name @paths = paths raise Error, "No paths provided" if paths.empty? @assets = paths.map do |path| asset = Asset[path] raise Error, "No such asset available: #{path}" unless asset asset end @extension = @assets.first.extension unless @assets.all? { |asset| asset.extension == @extension } raise Error, "All assets in a bundle must have the same extension" end @contents = bundle_type.process(@assets) @checksum = Digest::MD5.hexdigest(@contents) @mtime = @assets.map(&:mtime).max @length = Rack::Utils.bytesize(@contents) end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
14 15 16 |
# File 'lib/pinion/bundle.rb', line 14 def contents @contents end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/pinion/bundle.rb', line 14 def name @name end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
14 15 16 |
# File 'lib/pinion/bundle.rb', line 14 def paths @paths end |
Class Method Details
.[](name) ⇒ Object
Find a ‘Bundle` by its name.
51 |
# File 'lib/pinion/bundle.rb', line 51 def self.[](name) name && @@bundles[name.to_s] end |
.create(name, bundle_type_name, paths) ⇒ Object
Create a new bundle from a bundle_type name (e.g. ‘:concatenate_and_uglify_js`) and an array of paths. The name is taken as the identifier in the resulting path.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pinion/bundle.rb', line 39 def self.create(name, bundle_type_name, paths) bundle_type = BundleType[bundle_type_name] raise Error, "No such bundle type #{bundle_type_name}" unless bundle_type if @@bundles[name.to_s] raise Error, "There is already a bundle called #{name}. Each bundle must have a different name." end bundle = Bundle.new(bundle_type, name, paths) @@bundles[name.to_s] = bundle bundle end |