Class: Sprockets::StaticAsset
- Defined in:
- lib/sprockets/static_asset.rb
Overview
‘StaticAsset`s are used for files that are served verbatim without any processing or concatenation. These are typical images and other binary files.
Instance Attribute Summary
Attributes inherited from Asset
#environment, #id, #logical_path, #pathname
Class Method Summary collapse
-
.serialized_attributes ⇒ Object
Define extra attributes to be serialized.
Instance Method Summary collapse
-
#body ⇒ Object
Returns file contents as its ‘body`.
-
#fresh? ⇒ Boolean
Checks if Asset is fresh by comparing the actual mtime and digest to the inmemory model.
-
#initialize(environment, logical_path, pathname, digest = nil) ⇒ StaticAsset
constructor
A new instance of StaticAsset.
-
#to_path ⇒ Object
Implemented for Rack SendFile support.
-
#to_s ⇒ Object
‘to_s` is aliased to body since static assets can’t have any dependencies.
-
#write_to(filename, options = {}) ⇒ Object
Save asset to disk.
Methods inherited from Asset
#content_type, #dependencies, #digest, #digest_path, #each, #encode_with, #eql?, from_hash, #init_with, #inspect, #length, #mtime, #stale?, #to_a
Constructor Details
#initialize(environment, logical_path, pathname, digest = nil) ⇒ StaticAsset
Returns a new instance of StaticAsset.
15 16 17 18 19 |
# File 'lib/sprockets/static_asset.rb', line 15 def initialize(environment, logical_path, pathname, digest = nil) super(environment, logical_path, pathname) @digest = digest load! end |
Class Method Details
.serialized_attributes ⇒ Object
Define extra attributes to be serialized.
11 12 13 |
# File 'lib/sprockets/static_asset.rb', line 11 def self.serialized_attributes super + %w( content_type mtime length digest ) end |
Instance Method Details
#body ⇒ Object
Returns file contents as its ‘body`.
22 23 24 25 |
# File 'lib/sprockets/static_asset.rb', line 22 def body # File is read everytime to avoid memory bloat of large binary files pathname.open('rb') { |f| f.read } end |
#fresh? ⇒ Boolean
Checks if Asset is fresh by comparing the actual mtime and digest to the inmemory model.
29 30 31 32 |
# File 'lib/sprockets/static_asset.rb', line 29 def fresh? # Check current mtime and digest dependency_fresh?('path' => pathname, 'mtime' => mtime, 'hexdigest' => digest) end |
#to_path ⇒ Object
Implemented for Rack SendFile support.
35 36 37 |
# File 'lib/sprockets/static_asset.rb', line 35 def to_path pathname.to_s end |
#to_s ⇒ Object
‘to_s` is aliased to body since static assets can’t have any dependencies.
40 41 42 |
# File 'lib/sprockets/static_asset.rb', line 40 def to_s body end |
#write_to(filename, options = {}) ⇒ Object
Save asset to disk.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sprockets/static_asset.rb', line 45 def write_to(filename, = {}) # Gzip contents if filename has '.gz' [:compress] ||= File.extname(filename) == '.gz' if [:compress] # Open file and run it through `Zlib` pathname.open('rb') do |rd| File.open("#{filename}+", 'wb') do |wr| gz = Zlib::GzipWriter.new(wr, Zlib::BEST_COMPRESSION) buf = "" while rd.read(16384, buf) gz.write(buf) end gz.close end end else # If no compression needs to be done, we can just copy it into place. FileUtils.cp(pathname, "#{filename}+") end # Atomic write FileUtils.mv("#{filename}+", filename) # Set mtime correctly File.utime(mtime, mtime, filename) nil ensure # Ensure tmp file gets cleaned up FileUtils.rm("#{filename}+") if File.exist?("#{filename}+") end |