Object
`Asset` is the base class for `BundledAsset` and `StaticAsset`.
Internal initializer to load `Asset` from serialized `Hash`.
# File lib/sprockets/asset.rb, line 8 8: def self.from_hash(environment, hash) 9: return unless hash.is_a?(Hash) 10: 11: klass = case hash['class'] 12: when 'BundledAsset' 13: BundledAsset 14: when 'ProcessedAsset' 15: ProcessedAsset 16: when 'StaticAsset' 17: StaticAsset 18: else 19: nil 20: end 21: 22: if klass 23: asset = klass.allocate 24: asset.init_with(environment, hash) 25: asset 26: end 27: rescue UnserializeError 28: nil 29: end
# File lib/sprockets/asset.rb, line 34 34: def initialize(environment, logical_path, pathname) 35: @root = environment.root 36: @logical_path = logical_path.to_s 37: @pathname = Pathname.new(pathname) 38: @content_type = environment.content_type_of(pathname) 39: @mtime = environment.stat(pathname).mtime 40: @length = environment.stat(pathname).size 41: @digest = environment.file_digest(pathname).hexdigest 42: end
`body` is aliased to source by default if it can’t have any dependencies.
# File lib/sprockets/asset.rb, line 104 104: def body 105: source 106: end
Return an `Array` of `Asset` files that are declared dependencies.
# File lib/sprockets/asset.rb, line 88 88: def dependencies 89: [] 90: end
Return logical path with digest spliced in.
"foo/bar-37b51d194a7513e45b56f6524f2d51f2.js"
# File lib/sprockets/asset.rb, line 83 83: def digest_path 84: logical_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" } 85: end
Add enumerator to allow `Asset` instances to be used as Rack compatible body objects.
# File lib/sprockets/asset.rb, line 115 115: def each 116: yield to_s 117: end
Copy serialized attributes to the coder object
# File lib/sprockets/asset.rb, line 69 69: def encode_with(coder) 70: coder['class'] = self.class.name.sub(/Sprockets::/, '') 71: coder['logical_path'] = logical_path 72: coder['pathname'] = relativize_root_path(pathname).to_s 73: coder['content_type'] = content_type 74: coder['mtime'] = mtime.iso8601 75: coder['length'] = length 76: coder['digest'] = digest 77: end
Assets are equal if they share the same path, mtime and digest.
# File lib/sprockets/asset.rb, line 182 182: def eql?(other) 183: other.class == self.class && 184: other.logical_path == self.logical_path && 185: other.mtime.to_i == self.mtime.to_i && 186: other.digest == self.digest 187: end
Checks if Asset is fresh by comparing the actual mtime and digest to the inmemory model.
Used to test if cached models need to be rebuilt.
# File lib/sprockets/asset.rb, line 123 123: def fresh?(environment) 124: # Check current mtime and digest 125: dependency_fresh?(environment, self) 126: end
# File lib/sprockets/asset.rb, line 177 177: def hash 178: digest.hash 179: end
Initialize `Asset` from serialized `Hash`.
# File lib/sprockets/asset.rb, line 45 45: def init_with(environment, coder) 46: @root = environment.root 47: 48: @logical_path = coder['logical_path'] 49: @content_type = coder['content_type'] 50: @digest = coder['digest'] 51: 52: if pathname = coder['pathname'] 53: # Expand `$root` placeholder and wrapper string in a `Pathname` 54: @pathname = Pathname.new(expand_root_path(pathname)) 55: end 56: 57: if mtime = coder['mtime'] 58: # Parse time string 59: @mtime = Time.parse(mtime) 60: end 61: 62: if length = coder['length'] 63: # Convert length to an `Integer` 64: @length = Integer(length) 65: end 66: end
Pretty inspect
# File lib/sprockets/asset.rb, line 169 169: def inspect 170: "#<#{self.class}:0x#{object_id.to_s(16)} " + 171: "pathname=#{pathname.to_s.inspect}, " + 172: "mtime=#{mtime.inspect}, " + 173: "digest=#{digest.inspect}" + 174: ">" 175: end
Checks if Asset is stale by comparing the actual mtime and digest to the inmemory model.
Subclass must override `fresh?` or `stale?`.
# File lib/sprockets/asset.rb, line 132 132: def stale?(environment) 133: !fresh?(environment) 134: end
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.
# File lib/sprockets/asset.rb, line 99 99: def to_a 100: [self] 101: end
Return `String` of concatenated source.
# File lib/sprockets/asset.rb, line 109 109: def to_s 110: source 111: end
Save asset to disk.
# File lib/sprockets/asset.rb, line 137 137: def write_to(filename, options = {}) 138: # Gzip contents if filename has '.gz' 139: options[:compress] ||= File.extname(filename) == '.gz' 140: 141: FileUtils.mkdir_p File.dirname(filename) 142: 143: File.open("#{filename}+", 'wb') do |f| 144: if options[:compress] 145: # Run contents through `Zlib` 146: gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION) 147: gz.write to_s 148: gz.close 149: else 150: # Write out as is 151: f.write to_s 152: f.close 153: end 154: end 155: 156: # Atomic write 157: FileUtils.mv("#{filename}+", filename) 158: 159: # Set mtime correctly 160: File.utime(mtime, mtime, filename) 161: 162: nil 163: ensure 164: # Ensure tmp file gets cleaned up 165: FileUtils.rm("#{filename}+") if File.exist?("#{filename}+") 166: end
Check if dependency is fresh.
`dep` is a `Hash` with `path`, `mtime` and `hexdigest` keys.
A `Hash` is used rather than other `Asset` object because we want to test non-asset files and directories.
# File lib/sprockets/asset.rb, line 226 226: def dependency_fresh?(environment, dep) 227: path, mtime, hexdigest = dep.pathname.to_s, dep.mtime, dep.digest 228: 229: stat = environment.stat(path) 230: 231: # If path no longer exists, its definitely stale. 232: if stat.nil? 233: return false 234: end 235: 236: # Compare dependency mime to the actual mtime. If the 237: # dependency mtime is newer than the actual mtime, the file 238: # hasn't changed since we created this `Asset` instance. 239: # 240: # However, if the mtime is newer it doesn't mean the asset is 241: # stale. Many deployment environments may recopy or recheckout 242: # assets on each deploy. In this case the mtime would be the 243: # time of deploy rather than modified time. 244: if mtime >= stat.mtime 245: return true 246: end 247: 248: digest = environment.file_digest(path) 249: 250: # If the mtime is newer, do a full digest comparsion. Return 251: # fresh if the digests match. 252: if hexdigest == digest.hexdigest 253: return true 254: end 255: 256: # Otherwise, its stale. 257: false 258: end
Internal: String paths that are marked as dependencies after processing.
Default to an empty `Array`.
# File lib/sprockets/asset.rb, line 194 194: def dependency_paths 195: @dependency_paths ||= [] 196: end
Replace `$root` placeholder with actual environment root.
# File lib/sprockets/asset.rb, line 211 211: def expand_root_path(path) 212: path.to_s.sub(/^\$root/, @root) 213: end
Get pathname with its root stripped.
# File lib/sprockets/asset.rb, line 206 206: def relative_pathname 207: @relative_pathname ||= Pathname.new(relativize_root_path(pathname)) 208: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.