Parent

Sprockets::BundledAsset

`BundledAsset`s are used for files that need to be processed and concatenated with other assets. Use for `.js` and `.css` files.

Public Class Methods

new(environment, logical_path, pathname, options) click to toggle source
# File lib/sprockets/bundled_asset.rb, line 16
def initialize(environment, logical_path, pathname, options)
  super(environment, logical_path, pathname)
  @options = options || {}
end
serialized_attributes() click to toggle source

Define extra attributes to be serialized.

# File lib/sprockets/bundled_asset.rb, line 12
def self.serialized_attributes
  super + %( content_type mtime )
end

Public Instance Methods

body() click to toggle source

Get asset’s own processed contents. Excludes any of its required dependencies but does run any processors or engines on the original file.

# File lib/sprockets/bundled_asset.rb, line 55
def body
  @body ||= build_dependency_context_and_body[1]
end
dependencies() click to toggle source

Return an `Array` of `Asset` files that are declared dependencies.

# File lib/sprockets/bundled_asset.rb, line 75
def dependencies
  to_a - [self]
end
digest() click to toggle source

Compute digest of concatenated source.

# File lib/sprockets/bundled_asset.rb, line 70
def digest
  @digest ||= build_source['digest']
end
encode_with(coder) click to toggle source

Serialize custom attributes in `BundledAsset`.

# File lib/sprockets/bundled_asset.rb, line 42
def encode_with(coder)
  super

  coder['body']        = body
  coder['asset_paths'] = to_a.map { |a| relativize_root_path(a.pathname) }
  coder['dependency_paths'] = dependency_paths.map { |h|
    h.merge('path' => relativize_root_path(h['path']))
  }
end
fresh?() click to toggle source

Checks if Asset is stale by comparing the actual mtime and digest to the inmemory model.

# File lib/sprockets/bundled_asset.rb, line 86
def fresh?
  # Check freshness of all declared dependencies
  dependency_paths.all? { |h| dependency_fresh?(h) }
end
init_with(environment, coder) click to toggle source

Initialize `BundledAsset` from serialized `Hash`.

# File lib/sprockets/bundled_asset.rb, line 22
def init_with(environment, coder)
  @options = {}

  super

  @body   = coder['body']
  @assets = coder['asset_paths'].map { |p|
    p = expand_root_path(p)
    p == pathname.to_s ? self : environment[p, @options]
  }

  @dependency_paths = coder['dependency_paths'].map { |h|
    h.merge('path' => expand_root_path(h['path']))
  }
  @dependency_paths.each do |dep|
    dep['mtime'] = Time.parse(dep['mtime']) if dep['mtime'].is_a?(String)
  end
end
length() click to toggle source

Get size of concatenated source.

# File lib/sprockets/bundled_asset.rb, line 65
def length
  @length ||= build_source['length']
end
mtime() click to toggle source

Get latest mtime of all its dependencies.

# File lib/sprockets/bundled_asset.rb, line 60
def mtime
  @mtime ||= dependency_paths.map { |h| h['mtime'] }.max
end
to_a() click to toggle source

Expand asset into an `Array` of parts.

# File lib/sprockets/bundled_asset.rb, line 80
def to_a
  @assets ||= build_dependencies_paths_and_assets[1]
end
to_s() click to toggle source

Return `String` of concatenated source.

# File lib/sprockets/bundled_asset.rb, line 92
def to_s
  @source ||= build_source['source']
end
write_to(filename, options = {}) click to toggle source

Save asset to disk.

# File lib/sprockets/bundled_asset.rb, line 97
def write_to(filename, options = {})
  # Gzip contents if filename has '.gz'
  options[:compress] ||= File.extname(filename) == '.gz'

  File.open("#{filename}+", 'wb') do |f|
    if options[:compress]
      # Run contents through `Zlib`
      gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION)
      gz.write to_s
      gz.close
    else
      # Write out as is
      f.write to_s
      f.close
    end
  end

  # Atomic write
  FileUtils.mv("#{filename}+", filename)

  # Set mtime correctly
  File.utime(mtime, mtime, filename)

  nil
ensure
  # Ensure tmp file gets cleaned up
  FileUtils.rm("#{filename}+") if File.exist?("#{filename}+")
end

Protected Instance Methods

blank_context() click to toggle source

Return new blank `Context` to evaluate processors in.

# File lib/sprockets/bundled_asset.rb, line 128
def blank_context
  environment.context_class.new(environment, logical_path.to_s, pathname)
end
dependency_context() click to toggle source

Get `Context` after processors have been ran on it. This trackes any dependencies that processors have added to it.

# File lib/sprockets/bundled_asset.rb, line 134
def dependency_context
  @dependency_context ||= build_dependency_context_and_body[0]
end
dependency_paths() click to toggle source

All paths that this asset depends on. This list may include non-assets like directories.

# File lib/sprockets/bundled_asset.rb, line 140
def dependency_paths
  @dependency_paths ||= build_dependencies_paths_and_assets[0]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.