`BundledAsset`s are used for files that need to be processed and concatenated with other assets. Use for `.js` and `.css` files.
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
Return an `Array` of `Asset` files that are declared dependencies.
# File lib/sprockets/bundled_asset.rb, line 75 def dependencies to_a - [self] end
Compute digest of concatenated source.
# File lib/sprockets/bundled_asset.rb, line 70 def digest @digest ||= build_source['digest'] end
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
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
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
Get size of concatenated source.
# File lib/sprockets/bundled_asset.rb, line 65 def length @length ||= build_source['length'] end
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
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
Return `String` of concatenated source.
# File lib/sprockets/bundled_asset.rb, line 92 def to_s @source ||= build_source['source'] end
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
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
Generated with the Darkfish Rdoc Generator 2.