Parent

Sprockets::Asset

`Asset` is the base class for `BundledAsset` and `StaticAsset`.

Attributes

environment[R]
id[R]
logical_path[R]
pathname[R]

Public Class Methods

from_hash(environment, hash) click to toggle source

Internal initializer to load `Asset` from serialized `Hash`.

# File lib/sprockets/asset.rb, line 7
def self.from_hash(environment, hash)
  asset = allocate
  asset.init_with(environment, hash)
  asset
end
new(environment, logical_path, pathname) click to toggle source
# File lib/sprockets/asset.rb, line 21
def initialize(environment, logical_path, pathname)
  @environment  = environment
  @logical_path = logical_path.to_s
  @pathname     = Pathname.new(pathname)
  @id           = environment.digest.update(object_id.to_s).to_s
end
serialized_attributes() click to toggle source

Define base set of attributes to be serialized.

# File lib/sprockets/asset.rb, line 14
def self.serialized_attributes
  %( id logical_path pathname )
end

Public Instance Methods

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

Returns `Content-Type` from pathname.

# File lib/sprockets/asset.rb, line 71
def content_type
  @content_type ||= environment.content_type_of(pathname)
end
dependencies() click to toggle source

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

# File lib/sprockets/asset.rb, line 99
def dependencies
  []
end
digest() click to toggle source

Get content digest at the time the `Asset` is built.

# File lib/sprockets/asset.rb, line 86
def digest
  @digest ||= environment.file_digest(pathname).hexdigest
end
digest_path() click to toggle source

Return logical path with digest spliced in.

"foo/bar-37b51d194a7513e45b56f6524f2d51f2.js"
# File lib/sprockets/asset.rb, line 94
def digest_path
  environment.attributes_for(logical_path).path_with_fingerprint(digest)
end
each() click to toggle source

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

# File lib/sprockets/asset.rb, line 116
def each
  yield to_s
end
encode_with(coder) click to toggle source

Copy serialized attributes to the coder object

# File lib/sprockets/asset.rb, line 54
def encode_with(coder)
  coder['class'] = self.class.name.sub(/Sprockets::/, '')

  self.class.serialized_attributes.each do |attr|
    value = send(attr)
    coder[attr] = case value
      when Time
        value.iso8601
      else
        value.to_s
      end
  end

  coder['pathname'] = relativize_root_path(coder['pathname'])
end
eql?(other) click to toggle source

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

# File lib/sprockets/asset.rb, line 148
def eql?(other)
  other.class == self.class &&
    other.relative_pathname == self.relative_pathname &&
    other.mtime.to_i == self.mtime.to_i &&
    other.digest == self.digest
end
Also aliased as: ==
fresh?() click to toggle source

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

Used to test if cached models need to be rebuilt.

Subclass must override `fresh?` or `stale?`.

# File lib/sprockets/asset.rb, line 126
def fresh?
  !stale?
end
init_with(environment, coder) click to toggle source

Initialize `Asset` from serialized `Hash`.

# File lib/sprockets/asset.rb, line 29
def init_with(environment, coder)
  @environment = environment
  @pathname = @mtime = @length = nil

  self.class.serialized_attributes.each do |attr|
    instance_variable_set("@#{attr}", coder[attr].to_s) if coder[attr]
  end

  if @pathname && @pathname.is_a?(String)
    # Expand `$root` placeholder and wrapper string in a `Pathname`
    @pathname = Pathname.new(expand_root_path(@pathname))
  end

  if @mtime && @mtime.is_a?(String)
    # Parse time string
    @mtime = Time.parse(@mtime)
  end

  if @length && @length.is_a?(String)
    # Convert length to an `Integer`
    @length = Integer(@length)
  end
end
inspect() click to toggle source

Pretty inspect

# File lib/sprockets/asset.rb, line 139
def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} " +
    "pathname=#{pathname.to_s.inspect}, " +
    "mtime=#{mtime.inspect}, " +
    "digest=#{digest.inspect}" +
    ">"
end
length() click to toggle source

Get length at the time the `Asset` is built.

# File lib/sprockets/asset.rb, line 81
def length
  @length ||= environment.stat(pathname).size
end
mtime() click to toggle source

Get mtime at the time the `Asset` is built.

# File lib/sprockets/asset.rb, line 76
def mtime
  @mtime ||= environment.stat(pathname).mtime
end
stale?() click to toggle source

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

Subclass must override `fresh?` or `stale?`.

# File lib/sprockets/asset.rb, line 134
def stale?
  !fresh?
end
to_a() click to toggle source

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.

# File lib/sprockets/asset.rb, line 110
def to_a
  [self]
end

Protected Instance Methods

dependency_fresh?(dep = {}) click to toggle source

Check if dependency is fresh.

`dep` is a `Hash` with `path`, `mtime` and `hexdigest` keys.

A `Hash` is used rather than other `Asset` object because we want to test non-asset files and directories.

# File lib/sprockets/asset.rb, line 178
def dependency_fresh?(dep = {})
  path, mtime, hexdigest = dep.values_at('path', 'mtime', 'hexdigest')

  stat = environment.stat(path)

  # If path no longer exists, its definitely stale.
  if stat.nil?
    return false
  end

  # Compare dependency mime to the actual mtime. If the
  # dependency mtime is newer than the actual mtime, the file
  # hasn't changed since we created this `Asset` instance.
  #
  # However, if the mtime is newer it doesn't mean the asset is
  # stale. Many deployment environments may recopy or recheckout
  # assets on each deploy. In this case the mtime would be the
  # time of deploy rather than modified time.
  if mtime >= stat.mtime
    return true
  end

  digest = environment.file_digest(path)

  # If the mtime is newer, do a full digest comparsion. Return
  # fresh if the digests match.
  if hexdigest == digest.hexdigest
    return true
  end

  # Otherwise, its stale.
  false
end
expand_root_path(path) click to toggle source

Replace `$root` placeholder with actual environment root.

# File lib/sprockets/asset.rb, line 163
def expand_root_path(path)
  environment.attributes_for(path).expand_root
end
relative_pathname() click to toggle source

Get pathname with its root stripped.

# File lib/sprockets/asset.rb, line 158
def relative_pathname
  Pathname.new(relativize_root_path(pathname))
end
relativize_root_path(path) click to toggle source

Replace actual environment root with `$root` placeholder.

# File lib/sprockets/asset.rb, line 168
def relativize_root_path(path)
  environment.attributes_for(path).relativize_root
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.