Class: Sprockets::Asset

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, attributes = {}) ⇒ Asset

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.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sprockets/asset.rb', line 17

def initialize(environment, attributes = {})
  @environment  = environment
  @attributes   = attributes
  @content_type = attributes[:content_type]
  @filename     = attributes[:filename]
  @id           = attributes[:id]
  @integrity    = attributes[:integrity]
  @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

Instance Attribute Details

#content_typeObject (readonly)

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



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

def content_type
  @content_type
end

#filenameObject (readonly)

Public: Returns String path of asset.



50
51
52
# File 'lib/sprockets/asset.rb', line 50

def filename
  @filename
end

#idObject (readonly)

Internal: Unique asset object ID.

Returns a String.



62
63
64
# File 'lib/sprockets/asset.rb', line 62

def id
  @id
end

#integrityObject (readonly)

Public: A “named information” URL for subresource integrity.



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

def integrity
  @integrity
end

#logical_pathObject (readonly)

Returns the value of attribute logical_path



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

def logical_path
  @logical_path
end

#metadataObject (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.



47
48
49
# File 'lib/sprockets/asset.rb', line 47

def 
  @metadata
end

#uriObject (readonly)

Public: Internal URI to lookup asset by.

NOT a publically accessible URL.

Returns URI.



69
70
71
# File 'lib/sprockets/asset.rb', line 69

def uri
  @uri
end

Instance Method Details

#base64digestObject

Public: Returns String base64 digest of source.



194
195
196
# File 'lib/sprockets/asset.rb', line 194

def base64digest
  DigestUtils.pack_base64digest([:digest])
end

#charsetObject

Public: Get charset of source.

Returns a String charset name or nil if binary.



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

def charset
  [:charset]
end

#dependenciesObject

Deprecated: Get all required Assets.

See Asset#to_a

Returns Array of Assets.



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

def dependencies
  to_a.reject { |a| a.filename.eql?(self.filename) }
end

#digest_pathObject

Public: Return logical path with digest spliced in.

"foo/bar-37b51d194a7513e45b56f6524f2d51f2.js"

Returns String.



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

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

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

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)


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

def each
  yield to_s
end

#encodingObject

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”.



154
155
156
# File 'lib/sprockets/asset.rb', line 154

def encoding
  [:encoding]
end

#eql?(other) ⇒ Boolean Also known as: ==

Public: Compare assets.

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

Returns true or false.



253
254
255
# File 'lib/sprockets/asset.rb', line 253

def eql?(other)
  self.class == other.class && self.id == other.id
end

#hashObject

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

Returns Integer hash of the id.



244
245
246
# File 'lib/sprockets/asset.rb', line 244

def hash
  id.hash
end

#hexdigestObject Also known as: digest, etag

Public: Returns String hexdigest of source.



181
182
183
# File 'lib/sprockets/asset.rb', line 181

def hexdigest
  DigestUtils.pack_hexdigest([:digest])
end

#includedObject

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

Returns Array of String asset URIs.



96
97
98
# File 'lib/sprockets/asset.rb', line 96

def included
  [:included]
end

#inspectObject

Public: Pretty inspect

Returns String.



233
234
235
236
237
238
# File 'lib/sprockets/asset.rb', line 233

def inspect
  "#<#{self.class}:#{id} " +
    "filename=#{filename.inspect}, " +
    "digest=#{digest.inspect}" +
    ">"
end

#lengthObject Also known as: bytesize

Public: Returns Integer length of source.



166
167
168
# File 'lib/sprockets/asset.rb', line 166

def length
  [:length]
end

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.



88
89
90
# File 'lib/sprockets/asset.rb', line 88

def links
  [:links] || Set.new
end

#mtimeObject

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

Time resolution is normalized to the nearest second.

Returns Time.



176
177
178
# File 'lib/sprockets/asset.rb', line 176

def mtime
  Time.at(@mtime)
end

#pathnameObject

Deprecated: Use #filename instead.

Returns Pathname.



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

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

#sourceObject

Public: Return `String` of concatenated source.

Returns String.



133
134
135
136
137
138
139
140
# File 'lib/sprockets/asset.rb', line 133

def source
  if @source
    @source
  else
    # File is read everytime to avoid memory bloat of large binary files
    File.binread(filename)
  end
end

#to_aObject

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.



113
114
115
116
117
118
119
# File 'lib/sprockets/asset.rb', line 113

def to_a
  if [:included]
    [:included].map { |uri| @environment.load(uri) }
  else
    [self]
  end
end

#to_hashObject

Internal: Return all internal instance variables as a hash.

Returns a Hash.



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

def to_hash
  @attributes
end

#to_sObject

Public: Alias for #source.

Returns String.



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

def to_s
  source
end

#write_to(filename) ⇒ Object

Deprecated: Save asset to disk.

filename - String target

Returns nothing.



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/sprockets/asset.rb', line 217

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