Class: Sprockets::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/asset.rb

Overview

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

Direct Known Subclasses

BundledAsset, StaticAsset

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, logical_path, pathname) ⇒ Asset

Returns a new instance of Asset.



21
22
23
24
25
26
# 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

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



18
19
20
# File 'lib/sprockets/asset.rb', line 18

def environment
  @environment
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/sprockets/asset.rb', line 19

def id
  @id
end

#logical_pathObject (readonly)

Returns the value of attribute logical_path.



19
20
21
# File 'lib/sprockets/asset.rb', line 19

def logical_path
  @logical_path
end

#pathnameObject (readonly)

Returns the value of attribute pathname.



19
20
21
# File 'lib/sprockets/asset.rb', line 19

def pathname
  @pathname
end

Class Method Details

.from_hash(environment, hash) ⇒ Object

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



7
8
9
10
11
# File 'lib/sprockets/asset.rb', line 7

def self.from_hash(environment, hash)
  asset = allocate
  asset.init_with(environment, hash)
  asset
end

.serialized_attributesObject

Define base set of attributes to be serialized.



14
15
16
# File 'lib/sprockets/asset.rb', line 14

def self.serialized_attributes
  %w( id logical_path pathname )
end

Instance Method Details

#content_typeObject

Returns ‘Content-Type` from pathname.



71
72
73
# File 'lib/sprockets/asset.rb', line 71

def content_type
  @content_type ||= environment.content_type_of(pathname)
end

#dependenciesObject

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



99
100
101
# File 'lib/sprockets/asset.rb', line 99

def dependencies
  []
end

#digestObject

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



86
87
88
# File 'lib/sprockets/asset.rb', line 86

def digest
  @digest ||= environment.file_digest(pathname).hexdigest
end

#digest_pathObject

Return logical path with digest spliced in.

"foo/bar-37b51d194a7513e45b56f6524f2d51f2.js"


94
95
96
# File 'lib/sprockets/asset.rb', line 94

def digest_path
  environment.attributes_for(logical_path).path_with_fingerprint(digest)
end

#each {|to_s| ... } ⇒ Object

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

Yields:

  • (to_s)


116
117
118
# File 'lib/sprockets/asset.rb', line 116

def each
  yield to_s
end

#encode_with(coder) ⇒ Object

Copy serialized attributes to the coder object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Boolean Also known as: ==

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

Returns:

  • (Boolean)


148
149
150
151
152
153
# 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

#fresh?Boolean

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?`.

Returns:

  • (Boolean)


126
127
128
# File 'lib/sprockets/asset.rb', line 126

def fresh?
  !stale?
end

#init_with(environment, coder) ⇒ Object

Initialize ‘Asset` from serialized `Hash`.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

#inspectObject

Pretty inspect



139
140
141
142
143
144
145
# 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

#lengthObject

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



81
82
83
# File 'lib/sprockets/asset.rb', line 81

def length
  @length ||= environment.stat(pathname).size
end

#mtimeObject

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



76
77
78
# File 'lib/sprockets/asset.rb', line 76

def mtime
  @mtime ||= environment.stat(pathname).mtime
end

#stale?Boolean

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

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

Returns:

  • (Boolean)


134
135
136
# File 'lib/sprockets/asset.rb', line 134

def stale?
  !fresh?
end

#to_aObject

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.



110
111
112
# File 'lib/sprockets/asset.rb', line 110

def to_a
  [self]
end