class 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(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 17
def initialize(attributes = {})
  @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]
  @name         = attributes[:name]
  @source       = attributes[:source]
  @uri          = attributes[:uri]
end

Public Instance Methods

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

Public: Returns String base64 digest of source.

# File lib/sprockets/asset.rb, line 148
def base64digest
  DigestUtils.pack_base64digest(digest)
end
bytesize()
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 111
def charset
  metadata[:charset]
end
digest() click to toggle source

Public: Returns String byte digest of source.

# File lib/sprockets/asset.rb, line 122
def digest
  metadata[:digest]
end
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 66
def digest_path
  logical_path.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" }
end
each() { |to_s| ... } 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 164
def each
  yield to_s
end
environment_version() click to toggle source

Private: Return the version of the environment where the asset was generated.

# File lib/sprockets/asset.rb, line 127
def environment_version
  metadata[:environment_version]
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 203
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.

# File lib/sprockets/asset.rb, line 137
def etag
  version = environment_version

  if version && version != ""
    DigestUtils.hexdigest(version + digest)
  else
    DigestUtils.pack_hexdigest(digest)
  end
end
full_digest_path() click to toggle source

Public: Return load path + logical path with digest spliced in.

Returns String.

# File lib/sprockets/asset.rb, line 73
def full_digest_path
  File.join(@load_path, digest_path)
end
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 194
def hash
  id.hash
end
hexdigest() click to toggle source

Public: Returns String hexdigest of source.

# File lib/sprockets/asset.rb, line 132
def hexdigest
  DigestUtils.pack_hexdigest(digest)
end
inspect() click to toggle source

Public: Pretty inspect

Returns String.

# File lib/sprockets/asset.rb, line 186
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 153
def integrity
  DigestUtils.integrity_uri(digest)
end
length() click to toggle source

Public: Returns Integer length of source.

# File lib/sprockets/asset.rb, line 116
def length
  metadata[:length]
end
Also aliased as: bytesize
source() click to toggle source

Public: Return ‘String` of concatenated source.

Returns String.

# File lib/sprockets/asset.rb, line 92
def source
  if @source
    @source
  else
    # File is read everytime to avoid memory bloat of large binary files
    File.binread(filename)
  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 33
def to_hash
  @attributes
end
to_s() click to toggle source

Public: Alias for source.

Returns String.

# File lib/sprockets/asset.rb, line 104
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 173
def write_to(filename)
  FileUtils.mkdir_p File.dirname(filename)

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

  nil
end