Class: Sprockets::Asset

### Home Classes Methods

### In Files

- lib/sprockets/asset.rb
- lib/sprockets/legacy.rb

### Parent

Object

### Methods

- ::new
- #==
- #base64digest
- #bytesize
- #charset
- #dependencies
- #digest
- #digest_path
- #each
- #eql?
- #etag
- #hash
- #hexdigest
- #included
- #inspect
- #integrity
- #length
- #links
- #mtime
- #pathname
- #source
- #to_a
- #to_hash
- #to_s
- #write_to

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::Autoload
- Sprockets::Base
- Sprockets::Bower
- Sprockets::Bundle
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Cache::GetWrapper
- Sprockets::Cache::HashWrapper
- Sprockets::Cache::MemoryStore
- Sprockets::Cache::NullStore
- Sprockets::Cache::ReadWriteWrapper
- Sprockets::Cache::Wrapper
- Sprockets::CachedEnvironment
- Sprockets::ClosureCompressor
- Sprockets::CoffeeScriptProcessor
- Sprockets::CoffeeScriptTemplate
- Sprockets::Compressing
- Sprockets::Configuration
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::ConversionError
- Sprockets::Dependencies
- Sprockets::DigestUtils
- Sprockets::DirectiveProcessor
- Sprockets::ERBProcessor
- Sprockets::ERBTemplate
- Sprockets::EcoProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsProcessor
- Sprockets::EjsTemplate
- Sprockets::EncodingUtils
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::FileReader
- Sprockets::HTTPUtils
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::LegacyProcProcessor
- Sprockets::LegacyTiltProcessor
- Sprockets::Loader
- Sprockets::Manifest
- Sprockets::ManifestUtils
- Sprockets::Mime
- Sprockets::NotFound
- Sprockets::NotImplementedError
- Sprockets::PathDependencyUtils
- Sprockets::PathDigestUtils
- Sprockets::PathUtils
- Sprockets::Paths
- Sprockets::Processing
- Sprockets::ProcessorUtils
- Sprockets::Resolve
- Sprockets::SassCompressor
- Sprockets::SassProcessor
- Sprockets::SassProcessor::CacheStore
- Sprockets::SassProcessor::Functions
- Sprockets::SassTemplate
- Sprockets::ScssProcessor
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::Transformers
- Sprockets::URITar
- Sprockets::URIUtils
- Sprockets::UglifierCompressor
- Sprockets::UnloadedAsset
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::Asset

### Attributes

content_type[R]

Public: Returns String MIME type of asset. Returns nil if type is unknown.

filename[R]

Public: Returns String path of asset.

id[R]

Internal: Unique asset object ID.

Returns a String.

logical_path[R]
metadata[R]

Public: Metadata accumulated from pipeline process.

The API status of the keys is dependent on the pipeline processors itself. So some values maybe considered public and others internal. See the pipeline proccessor documentation itself.

Returns Hash.

uri[R]

Public: Internal URI to lookup asset by.

NOT a publically accessible URL.

Returns URI.

### Public Class Methods

new(environment, attributes = {}) click to toggle source

Private: Intialize Asset wrapper from attributes Hash.

Asset wrappers should not be initialized directly, only Environment#find_asset should vend them.

attributes - Hash of ivars

Returns Asset.

    # File lib/sprockets/asset.rb, line 16
    def initialize(environment, attributes = {})
      @environment  = environment
      @attributes   = attributes
      @content_type = attributes[:content_type]
      @filename     = attributes[:filename]
      @id           = attributes[:id]
      @load_path    = attributes[:load_path]
      @logical_path = attributes[:logical_path]
      @metadata     = attributes[:metadata]
      @mtime        = attributes[:mtime]
      @name         = attributes[:name]
      @source       = attributes[:source]
      @uri          = attributes[:uri]
    end

### Public Instance Methods

==(other) click to toggle source
Alias for: eql?
base64digest() click to toggle source

Public: Returns String base64 digest of source.

    # File lib/sprockets/asset.rb, line 137
    def base64digest
      DigestUtils.pack_base64digest(metadata[:digest])
    end

bytesize() click to toggle source
Alias for: length
charset() click to toggle source

Public: Get charset of source.

Returns a String charset name or nil if binary.

    # File lib/sprockets/asset.rb, line 113
    def charset
      metadata[:charset]
    end

dependencies() click to toggle source

Deprecated: Get all required Assets.

See Asset#to_a

Returns Array of Assets.

    # File lib/sprockets/legacy.rb, line 206
    def dependencies
      to_a.reject { |a| a.filename.eql?(self.filename) }
    end

digest() click to toggle source

Deprecated: Returns String hexdigest of source.

In 4.x this will be changed to return a raw Digest byte String.

Alias for: hexdigest
digest_path() click to toggle source

Public: Return logical path with digest spliced in.

    "foo/bar-37b51d194a7513e45b56f6524f2d51f2.js"

Returns String.

    # File lib/sprockets/asset.rb, line 67
    def digest_path
      logical_path.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" }
    end

each() click to toggle source

Public: Add enumerator to allow `Asset` instances to be used as Rack compatible body objects.

block

    part - String body chunk

Returns nothing.

    # File lib/sprockets/asset.rb, line 153
    def each
      yield to_s
    end

eql?(other) click to toggle source

Public: Compare assets.

Assets are equal if they share the same path and digest.

Returns true or false.

    # File lib/sprockets/asset.rb, line 195
    def eql?(other)
      self.class == other.class && self.id == other.id
    end

Also aliased as: ==
etag() click to toggle source

Pubic: ETag String of Asset.

Alias for: hexdigest
hash() click to toggle source

Public: Implements Object#hash so Assets can be used as a Hash key or in a Set.

Returns Integer hash of the id.

    # File lib/sprockets/asset.rb, line 186
    def hash
      id.hash
    end

hexdigest() click to toggle source

Public: Returns String hexdigest of source.

    # File lib/sprockets/asset.rb, line 124
    def hexdigest
      DigestUtils.pack_hexdigest(metadata[:digest])
    end

Also aliased as: digest, etag
included() click to toggle source

Public: Get all internally required assets that were concated into this asset.

Returns Array of String asset URIs.

    # File lib/sprockets/asset.rb, line 87
    def included
      metadata[:included]
    end

inspect() click to toggle source

Public: Pretty inspect

Returns String.

    # File lib/sprockets/asset.rb, line 178
    def inspect
      "#<#{self.class}:#{object_id.to_s(16)} #{uri.inspect}>"
    end

integrity() click to toggle source

Public: A "named information" URL for subresource integrity.

    # File lib/sprockets/asset.rb, line 142
    def integrity
      DigestUtils.integrity_uri(metadata[:digest])
    end

length() click to toggle source

Public: Returns Integer length of source.

    # File lib/sprockets/asset.rb, line 118
    def length
      metadata[:length]
    end

Also aliased as: bytesize
links() click to toggle source

Public: Get all externally linked asset filenames from asset.

All linked assets should be compiled anytime this asset is.

Returns Set of String asset URIs.

    # File lib/sprockets/asset.rb, line 79
    def links
      metadata[:links] || Set.new
    end

mtime() click to toggle source

Deprecated: Returns Time of the last time the source was modified.

Time resolution is normalized to the nearest second.

Returns Time.

    # File lib/sprockets/legacy.rb, line 215
    def mtime
      Time.at(@mtime)
    end

pathname() click to toggle source

Deprecated: Use filename instead.

Returns Pathname.

    # File lib/sprockets/legacy.rb, line 176
    def pathname
      @pathname ||= Pathname.new(filename)
    end

source() click to toggle source

Public: Return `String` of concatenated source.

Returns String.

    # File lib/sprockets/asset.rb, line 94
    def source
      if @source
        @source
      else
        # File is read everytime to avoid memory bloat of large binary files
        File.binread(filename)
      end
    end

to_a() click to toggle source

Deprecated: Expand asset into an `Array` of parts.

Appending all of an assets body parts together should give you the asset's contents as a whole.

This allows you to link to individual files for debugging purposes.

Use Asset#included instead. Keeping a full copy of the bundle's processed assets in memory (and in cache) is expensive and redundant. The common use case is to relink to the assets anyway.

Returns Array of Assets.

    # File lib/sprockets/legacy.rb, line 193
    def to_a
      if metadata[:included]
        metadata[:included].map { |uri| @environment.load(uri) }
      else
        [self]
      end
    end

to_hash() click to toggle source

Internal: Return all internal instance variables as a hash.

Returns a Hash.

    # File lib/sprockets/asset.rb, line 34
    def to_hash
      @attributes
    end

to_s() click to toggle source

Public: Alias for source.

Returns String.

    # File lib/sprockets/asset.rb, line 106
    def to_s
      source
    end

write_to(filename) click to toggle source

Deprecated: Save asset to disk.

filename - String target

Returns nothing.

    # File lib/sprockets/asset.rb, line 162
    def write_to(filename)
      FileUtils.mkdir_p File.dirname(filename)

      PathUtils.atomic_write(filename) do |f|
        f.write source
      end

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

      nil
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.