Class: Sprockets::Asset
- Inherits:
-
Object
- Object
- Sprockets::Asset
- Defined in:
- lib/sprockets/asset.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.
-
#digest ⇒ Object
Public: Returns String byte digest of source.
-
#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.
-
#environment_version ⇒ Object
Private: Return the version of the environment where the asset was generated.
-
#eql?(other) ⇒ Boolean
(also: #==)
Public: Compare assets.
-
#etag ⇒ Object
Public: ETag String of Asset.
-
#full_digest_path ⇒ Object
Public: Return load path + logical path with digest spliced in.
-
#hash ⇒ Object
Public: Implements Object#hash so Assets can be used as a Hash key or in a Set.
-
#hexdigest ⇒ Object
Public: Returns String hexdigest of source.
-
#initialize(attributes = {}) ⇒ Asset
constructor
Private: Initialize 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.
-
#source ⇒ Object
Public: Return ‘String` of concatenated source.
-
#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(attributes = {}) ⇒ Asset
Private: Initialize 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_type ⇒ Object (readonly)
Public: Returns String MIME type of asset. Returns nil if type is unknown.
82 83 84 |
# File 'lib/sprockets/asset.rb', line 82 def content_type @content_type end |
#filename ⇒ Object (readonly)
Public: Returns String path of asset.
47 48 49 |
# File 'lib/sprockets/asset.rb', line 47 def filename @filename end |
#id ⇒ Object (readonly)
Internal: Unique asset object ID.
Returns a String.
52 53 54 |
# File 'lib/sprockets/asset.rb', line 52 def id @id end |
#logical_path ⇒ Object (readonly)
Returns the value of attribute logical_path.
7 8 9 |
# File 'lib/sprockets/asset.rb', line 7 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 processor documentation itself.
Returns Hash.
44 45 46 |
# File 'lib/sprockets/asset.rb', line 44 def @metadata end |
#uri ⇒ Object (readonly)
Public: Internal URI to lookup asset by.
NOT a publicly accessible URL.
Returns URI.
59 60 61 |
# File 'lib/sprockets/asset.rb', line 59 def uri @uri end |
Instance Method Details
#base64digest ⇒ Object
Public: Returns String base64 digest of source.
152 153 154 |
# File 'lib/sprockets/asset.rb', line 152 def base64digest DigestUtils.pack_base64digest(digest) end |
#charset ⇒ Object
Public: Get charset of source.
Returns a String charset name or nil if binary.
115 116 117 |
# File 'lib/sprockets/asset.rb', line 115 def charset [:charset] end |
#digest ⇒ Object
Public: Returns String byte digest of source.
126 127 128 |
# File 'lib/sprockets/asset.rb', line 126 def digest [:digest] end |
#digest_path ⇒ Object
Public: Return logical path with digest spliced in.
"foo/bar-37b51d194a7513e45b56f6524f2d51f2.js"
Returns String.
66 67 68 69 70 71 72 |
# File 'lib/sprockets/asset.rb', line 66 def digest_path if DigestUtils.already_digested?(@name) logical_path else logical_path.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" } end 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.
168 169 170 |
# File 'lib/sprockets/asset.rb', line 168 def each yield to_s end |
#environment_version ⇒ Object
Private: Return the version of the environment where the asset was generated.
131 132 133 |
# File 'lib/sprockets/asset.rb', line 131 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.
207 208 209 |
# File 'lib/sprockets/asset.rb', line 207 def eql?(other) self.class == other.class && self.id == other.id end |
#etag ⇒ Object
Public: ETag String of Asset.
141 142 143 144 145 146 147 148 149 |
# File 'lib/sprockets/asset.rb', line 141 def etag version = environment_version if version && version != "" DigestUtils.hexdigest(version + digest) else DigestUtils.pack_hexdigest(digest) end end |
#full_digest_path ⇒ Object
Public: Return load path + logical path with digest spliced in.
Returns String.
77 78 79 |
# File 'lib/sprockets/asset.rb', line 77 def full_digest_path File.join(@load_path, digest_path) 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.
198 199 200 |
# File 'lib/sprockets/asset.rb', line 198 def hash id.hash end |
#hexdigest ⇒ Object
Public: Returns String hexdigest of source.
136 137 138 |
# File 'lib/sprockets/asset.rb', line 136 def hexdigest DigestUtils.pack_hexdigest(digest) end |
#inspect ⇒ Object
Public: Pretty inspect
Returns String.
190 191 192 |
# File 'lib/sprockets/asset.rb', line 190 def inspect "#<#{self.class}:#{object_id.to_s(16)} #{uri.inspect}>" end |
#integrity ⇒ Object
Public: A “named information” URL for subresource integrity.
157 158 159 |
# File 'lib/sprockets/asset.rb', line 157 def integrity DigestUtils.integrity_uri(digest) end |
#length ⇒ Object Also known as: bytesize
Public: Returns Integer length of source.
120 121 122 |
# File 'lib/sprockets/asset.rb', line 120 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.
89 90 91 |
# File 'lib/sprockets/asset.rb', line 89 def links [:links] || Set.new end |
#source ⇒ Object
Public: Return ‘String` of concatenated source.
Returns String.
96 97 98 99 100 101 102 103 |
# File 'lib/sprockets/asset.rb', line 96 def source if @source @source else # File is read everytime to avoid memory bloat of large binary files File.binread(filename) end end |
#to_hash ⇒ Object
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_s ⇒ Object
Public: Alias for #source.
Returns String.
108 109 110 |
# File 'lib/sprockets/asset.rb', line 108 def to_s source end |
#write_to(filename) ⇒ Object
Deprecated: Save asset to disk.
filename - String target
Returns nothing.
177 178 179 180 181 182 183 184 185 |
# File 'lib/sprockets/asset.rb', line 177 def write_to(filename) FileUtils.mkdir_p File.dirname(filename) PathUtils.atomic_write(filename) do |f| f.write source end nil end |