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

Instance Attribute Details

#content_typeObject (readonly)

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



78
79
80
# File 'lib/sprockets/asset.rb', line 78

def content_type
  @content_type
end

#filenameObject (readonly)

Public: Returns String path of asset.



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

def filename
  @filename
end

#idObject (readonly)

Internal: Unique asset object ID.

Returns a String.



52
53
54
# File 'lib/sprockets/asset.rb', line 52

def id
  @id
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.



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

def 
  @metadata
end

#uriObject (readonly)

Public: Internal URI to lookup asset by.

NOT a publically accessible URL.

Returns URI.



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

def uri
  @uri
end

Instance Method Details

#base64digestObject

Public: Returns String base64 digest of source.



142
143
144
# File 'lib/sprockets/asset.rb', line 142

def base64digest
  DigestUtils.pack_base64digest(digest)
end

#charsetObject

Public: Get charset of source.

Returns a String charset name or nil if binary.



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

def charset
  [:charset]
end

#digestObject

Public: Returns String byte digest of source.



122
123
124
# File 'lib/sprockets/asset.rb', line 122

def digest
  [:digest]
end

#digest_pathObject

Public: Return logical path with digest spliced in.

"foo/bar-37b51d194a7513e45b56f6524f2d51f2.js"

Returns String.



66
67
68
# File 'lib/sprockets/asset.rb', line 66

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:



158
159
160
# File 'lib/sprockets/asset.rb', line 158

def each
  yield to_s
end

#environment_versionObject

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



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

def environment_version
  [:environment_version]
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.

Returns:

  • (Boolean)


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

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

#etagObject

Pubic: ETag String of Asset.



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

def etag
  DigestUtils.pack_hexdigest(environment_version + digest)
end

#full_digest_pathObject

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

Returns String.



73
74
75
# File 'lib/sprockets/asset.rb', line 73

def full_digest_path
  File.join(@load_path, digest_path)
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.



188
189
190
# File 'lib/sprockets/asset.rb', line 188

def hash
  id.hash
end

#hexdigestObject

Public: Returns String hexdigest of source.



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

def hexdigest
  DigestUtils.pack_hexdigest(digest)
end

#inspectObject

Public: Pretty inspect

Returns String.



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

def inspect
  "#<#{self.class}:#{object_id.to_s(16)} #{uri.inspect}>"
end

#integrityObject

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



147
148
149
# File 'lib/sprockets/asset.rb', line 147

def integrity
  DigestUtils.integrity_uri(digest)
end

#lengthObject Also known as: bytesize

Public: Returns Integer length of source.



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

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.



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

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

#sourceObject

Public: Return ‘String` of concatenated source.

Returns String.



92
93
94
95
96
97
98
99
# 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_hashObject

Internal: Return all internal instance variables as a hash.

Returns a Hash.



33
34
35
# File 'lib/sprockets/asset.rb', line 33

def to_hash
  @attributes
end

#to_sObject

Public: Alias for #source.

Returns String.



104
105
106
# File 'lib/sprockets/asset.rb', line 104

def to_s
  source
end

#write_to(filename) ⇒ Object

Deprecated: Save asset to disk.

filename - String target

Returns nothing.



167
168
169
170
171
172
173
174
175
# File 'lib/sprockets/asset.rb', line 167

def write_to(filename)
  FileUtils.mkdir_p File.dirname(filename)

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

  nil
end