Class: Pinion::Bundle

Inherits:
Asset
  • Object
show all
Defined in:
lib/pinion/bundle.rb

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

Attributes inherited from Asset

#checksum, #extension, #length, #mtime

Class Method Summary collapse

Instance Method Summary collapse

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`.

Raises:



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

#contentsObject (readonly)

Returns the value of attribute contents.



14
15
16
# File 'lib/pinion/bundle.rb', line 14

def contents
  @contents
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/pinion/bundle.rb', line 14

def name
  @name
end

#pathsObject (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.

Raises:



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