Class: Sprockets::Asset

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Asset) initialize(attributes = {})

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.



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

def initialize(attributes = {})
  @attributes = attributes
  attributes.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
end

Instance Attribute Details

- (Object) charset (readonly)

Public: Get charset of source.

Returns a String charset name or nil if binary.



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

def charset
  @charset
end

- (Object) content_type (readonly)

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



59
60
61
# File 'lib/sprockets/asset.rb', line 59

def content_type
  @content_type
end

- (Object) digest (readonly) Also known as: etag

Public: Returns String hexdigest of source.



149
150
151
# File 'lib/sprockets/asset.rb', line 149

def digest
  @digest
end

- (Object) filename (readonly)

Public: Returns String path of asset.



40
41
42
# File 'lib/sprockets/asset.rb', line 40

def filename
  @filename
end

- (Object) length (readonly) Also known as: bytesize

Public: Returns Integer length of source.



136
137
138
# File 'lib/sprockets/asset.rb', line 136

def length
  @length
end

- (Object) logical_path (readonly)

Returns the value of attribute logical_path



6
7
8
# File 'lib/sprockets/asset.rb', line 6

def logical_path
  @logical_path
end

- (Object) metadata (readonly)

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.



37
38
39
# File 'lib/sprockets/asset.rb', line 37

def 
  @metadata
end

Instance Method Details

- (Object) digest_path

Public: Return logical path with digest spliced in.

"foo/bar-37b51d194a7513e45b56f6524f2d51f2.js"

Returns String.



54
55
56
# File 'lib/sprockets/asset.rb', line 54

def digest_path
  logical_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
end

- (Object) each {|to_s| ... }

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

block

part - String body chunk

Returns nothing.

Yields:

  • (to_s)


161
162
163
# File 'lib/sprockets/asset.rb', line 161

def each
  yield to_s
end

- (Object) encoding

Public: HTTP encoding for Asset, “deflate”, “gzip”, etc.

Note: This is not the Ruby Encoding of the source. See Asset#charset.

Returns a String or nil if encoding is “identity”.



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

def encoding
  [:encoding]
end

- (Boolean) eql?(other) Also known as: ==

Public: Compare assets.

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

Returns true or false.



207
208
209
210
211
212
# File 'lib/sprockets/asset.rb', line 207

def eql?(other)
  other.class == self.class &&
    other.filename == self.filename &&
    other.mtime.to_i == self.mtime.to_i &&
    other.digest == self.digest
end

- (Object) hash

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

Returns Integer hash of digest.



198
199
200
# File 'lib/sprockets/asset.rb', line 198

def hash
  digest.hash
end

- (Object) inspect

Public: Pretty inspect

Returns String.



186
187
188
189
190
191
192
# File 'lib/sprockets/asset.rb', line 186

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} " +
    "filename=#{filename.inspect}, " +
    "mtime=#{mtime.inspect}, " +
    "digest=#{digest.inspect}" +
    ">"
end

- (Object) mtime

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

Time resolution is normalized to the nearest second.

Returns Time.



144
145
146
# File 'lib/sprockets/asset.rb', line 144

def mtime
  Time.at(@mtime)
end

- (Object) pathname

Deprecated: Use #filename instead.

Returns Pathname.



45
46
47
# File 'lib/sprockets/asset.rb', line 45

def pathname
  @pathname ||= Pathname.new(filename)
end

- (Object) source

Public: Return `String` of concatenated source.

Returns String.



105
106
107
108
109
110
111
112
# File 'lib/sprockets/asset.rb', line 105

def source
  if defined? @source
    @source
  else
    # File is read everytime to avoid memory bloat of large binary files
    File.open(filename, 'rb') { |f| f.read }
  end
end

- (Object) source_paths

Public: Array of required processed assets.

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

Examples

asset.source_paths #=>
["jquery-729a810640240adfd653c3d958890cfc4ec0ea84.js",
 "users-08ae3439d6c8fe911445a2fb6e07ee1dc12ca599.js",
 "application-b5df367abb741cac6526b05a726e9e8d7bd863d2.js"]

Returns an Array of String digest paths.



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

def source_paths
  to_a.map(&:digest_path)
end

- (Object) to_a

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#source_paths 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. #source_paths provides that reference.

Returns Array of Assets.



75
76
77
78
79
80
81
82
83
# File 'lib/sprockets/asset.rb', line 75

def to_a
  if .key?(:required_asset_hashes)
    [:required_asset_hashes].map do |hash|
      Asset.new(hash)
    end
  else
    [self]
  end
end

- (Object) to_hash

Internal: Return all internal instance variables as a hash.

Returns a Hash.



26
27
28
# File 'lib/sprockets/asset.rb', line 26

def to_hash
  @attributes
end

- (Object) to_s

Public: Alias for #source.

Returns String.



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

def to_s
  source
end

- (Object) write_to(filename)

Public: Save asset to disk.

filename - String target

Returns nothing.



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/sprockets/asset.rb', line 170

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