Class: Sprockets::Asset
- Inherits:
-
Object
- Object
- Sprockets::Asset
- Defined in:
- lib/sprockets/asset.rb,
lib/sprockets/legacy.rb
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Public: Returns String MIME type of asset.
-
#filename ⇒ Object
readonly
Public: Returns String path of asset.
-
#id ⇒ Object
readonly
Internal: Unique asset object ID.
-
#logical_path ⇒ Object
readonly
Returns the value of attribute logical_path.
-
#metadata ⇒ Object
readonly
Public: Metadata accumulated from pipeline process.
-
#uri ⇒ Object
readonly
Public: Internal URI to lookup asset by.
Instance Method Summary collapse
-
#base64digest ⇒ Object
Public: Returns String base64 digest of source.
-
#charset ⇒ Object
Public: Get charset of source.
-
#dependencies ⇒ Object
Deprecated: Get all required Assets.
-
#digest_path ⇒ Object
Public: Return logical path with digest spliced in.
-
#each {|to_s| ... } ⇒ Object
Public: Add enumerator to allow ‘Asset` instances to be used as Rack compatible body objects.
-
#eql?(other) ⇒ Boolean
(also: #==)
Public: Compare assets.
-
#hash ⇒ Object
Public: Implements Object#hash so Assets can be used as a Hash key or in a Set.
-
#hexdigest ⇒ Object
(also: #digest, #etag)
Public: Returns String hexdigest of source.
-
#included ⇒ Object
Public: Get all internally required assets that were concated into this asset.
-
#initialize(environment, attributes = {}) ⇒ Asset
constructor
Private: Intialize Asset wrapper from attributes Hash.
-
#inspect ⇒ Object
Public: Pretty inspect.
-
#integrity ⇒ Object
Public: A “named information” URL for subresource integrity.
-
#length ⇒ Object
(also: #bytesize)
Public: Returns Integer length of source.
-
#links ⇒ Object
Public: Get all externally linked asset filenames from asset.
-
#mtime ⇒ Object
Deprecated: Returns Time of the last time the source was modified.
-
#pathname ⇒ Object
Deprecated: Use #filename instead.
-
#source ⇒ Object
Public: Return ‘String` of concatenated source.
-
#to_a ⇒ Object
Deprecated: Expand asset into an ‘Array` of parts.
-
#to_hash ⇒ Object
Internal: Return all internal instance variables as a hash.
-
#to_s ⇒ Object
Public: Alias for #source.
-
#write_to(filename) ⇒ Object
Deprecated: Save asset to disk.
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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sprockets/asset.rb', line 16 def initialize(environment, attributes = {}) @environment = environment @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] @mtime = attributes[:mtime] @name = attributes[:name] @source = attributes[:source] @uri = attributes[:uri] end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Public: Returns String MIME type of asset. Returns nil if type is unknown.
72 73 74 |
# File 'lib/sprockets/asset.rb', line 72 def content_type @content_type end |
#filename ⇒ Object (readonly)
Public: Returns String path of asset.
48 49 50 |
# File 'lib/sprockets/asset.rb', line 48 def filename @filename end |
#id ⇒ Object (readonly)
Internal: Unique asset object ID.
Returns a String.
53 54 55 |
# File 'lib/sprockets/asset.rb', line 53 def id @id end |
#logical_path ⇒ Object (readonly)
Returns the value of attribute logical_path.
6 7 8 |
# File 'lib/sprockets/asset.rb', line 6 def logical_path @logical_path end |
#metadata ⇒ Object (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.
45 46 47 |
# File 'lib/sprockets/asset.rb', line 45 def @metadata end |
#uri ⇒ Object (readonly)
Public: Internal URI to lookup asset by.
NOT a publically accessible URL.
Returns URI.
60 61 62 |
# File 'lib/sprockets/asset.rb', line 60 def uri @uri end |
Instance Method Details
#base64digest ⇒ Object
Public: Returns String base64 digest of source.
137 138 139 |
# File 'lib/sprockets/asset.rb', line 137 def base64digest DigestUtils.pack_base64digest([:digest]) end |
#charset ⇒ Object
Public: Get charset of source.
Returns a String charset name or nil if binary.
113 114 115 |
# File 'lib/sprockets/asset.rb', line 113 def charset [:charset] end |
#dependencies ⇒ Object
Deprecated: Get all required Assets.
See Asset#to_a
Returns Array of Assets.
206 207 208 |
# File 'lib/sprockets/legacy.rb', line 206 def dependencies to_a.reject { |a| a.filename.eql?(self.filename) } end |
#digest_path ⇒ Object
Public: Return logical path with digest spliced in.
"foo/bar-37b51d194a7513e45b56f6524f2d51f2.js"
Returns String.
67 68 69 |
# File 'lib/sprockets/asset.rb', line 67 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.
153 154 155 |
# File 'lib/sprockets/asset.rb', line 153 def each yield to_s 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.
195 196 197 |
# File 'lib/sprockets/asset.rb', line 195 def eql?(other) self.class == other.class && self.id == other.id end |
#hash ⇒ Object
Public: Implements Object#hash so Assets can be used as a Hash key or in a Set.
Returns Integer hash of the id.
186 187 188 |
# File 'lib/sprockets/asset.rb', line 186 def hash id.hash end |
#hexdigest ⇒ Object Also known as: digest, etag
Public: Returns String hexdigest of source.
124 125 126 |
# File 'lib/sprockets/asset.rb', line 124 def hexdigest DigestUtils.pack_hexdigest([:digest]) end |
#included ⇒ Object
Public: Get all internally required assets that were concated into this asset.
Returns Array of String asset URIs.
87 88 89 |
# File 'lib/sprockets/asset.rb', line 87 def included [:included] end |
#inspect ⇒ Object
Public: Pretty inspect
Returns String.
178 179 180 |
# File 'lib/sprockets/asset.rb', line 178 def inspect "#<#{self.class}:#{object_id.to_s(16)} #{uri.inspect}>" end |
#integrity ⇒ Object
Public: A “named information” URL for subresource integrity.
142 143 144 |
# File 'lib/sprockets/asset.rb', line 142 def integrity DigestUtils.integrity_uri([:digest]) end |
#length ⇒ Object Also known as: bytesize
Public: Returns Integer length of source.
118 119 120 |
# File 'lib/sprockets/asset.rb', line 118 def length [:length] end |
#links ⇒ Object
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.
79 80 81 |
# File 'lib/sprockets/asset.rb', line 79 def links [:links] || Set.new end |
#mtime ⇒ Object
Deprecated: Returns Time of the last time the source was modified.
Time resolution is normalized to the nearest second.
Returns Time.
215 216 217 |
# File 'lib/sprockets/legacy.rb', line 215 def mtime Time.at(@mtime) end |
#pathname ⇒ Object
Deprecated: Use #filename instead.
Returns Pathname.
176 177 178 |
# File 'lib/sprockets/legacy.rb', line 176 def pathname @pathname ||= Pathname.new(filename) end |
#source ⇒ Object
Public: Return ‘String` of concatenated source.
Returns String.
94 95 96 97 98 99 100 101 |
# File 'lib/sprockets/asset.rb', line 94 def source if @source @source else # File is read everytime to avoid memory bloat of large binary files File.binread(filename) end end |
#to_a ⇒ Object
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.
193 194 195 196 197 198 199 |
# File 'lib/sprockets/legacy.rb', line 193 def to_a if [:included] [:included].map { |uri| @environment.load(uri) } else [self] end end |
#to_hash ⇒ Object
Internal: Return all internal instance variables as a hash.
Returns a Hash.
34 35 36 |
# File 'lib/sprockets/asset.rb', line 34 def to_hash @attributes end |
#to_s ⇒ Object
Public: Alias for #source.
Returns String.
106 107 108 |
# File 'lib/sprockets/asset.rb', line 106 def to_s source end |
#write_to(filename) ⇒ Object
Deprecated: Save asset to disk.
filename - String target
Returns nothing.
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/sprockets/asset.rb', line 162 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 |