Class: Spar::CompiledAsset
- Inherits:
-
Object
- Object
- Spar::CompiledAsset
- Defined in:
- lib/spar/compiled_asset.rb
Instance Attribute Summary collapse
-
#asset ⇒ Object
Returns the value of attribute asset.
-
#digest ⇒ Object
Returns the value of attribute digest.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#logical_path ⇒ Object
Returns the value of attribute logical_path.
-
#mtime ⇒ Object
Returns the value of attribute mtime.
-
#write_path ⇒ Object
Returns the value of attribute write_path.
Instance Method Summary collapse
- #data ⇒ Object
- #generate_headers(options) ⇒ Object
- #generate_write_path(options) ⇒ Object
-
#initialize(asset, options = {}) ⇒ CompiledAsset
constructor
A new instance of CompiledAsset.
- #write_to(base_path, options = {}) ⇒ Object
Constructor Details
#initialize(asset, options = {}) ⇒ CompiledAsset
Returns a new instance of CompiledAsset.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/spar/compiled_asset.rb', line 9 def initialize(asset, ={}) if asset.is_a?(String) @file = File.new(asset, 'rb') @logical_path = asset else @asset = asset @logical_path = @asset.logical_path end @write_path = generate_write_path() @headers = generate_headers() @mtime = (@asset || @file).mtime @digest = @asset ? @asset.digest : Digest::MD5.hexdigest(data) end |
Instance Attribute Details
#asset ⇒ Object
Returns the value of attribute asset.
7 8 9 |
# File 'lib/spar/compiled_asset.rb', line 7 def asset @asset end |
#digest ⇒ Object
Returns the value of attribute digest.
7 8 9 |
# File 'lib/spar/compiled_asset.rb', line 7 def digest @digest end |
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/spar/compiled_asset.rb', line 7 def headers @headers end |
#logical_path ⇒ Object
Returns the value of attribute logical_path.
7 8 9 |
# File 'lib/spar/compiled_asset.rb', line 7 def logical_path @logical_path end |
#mtime ⇒ Object
Returns the value of attribute mtime.
7 8 9 |
# File 'lib/spar/compiled_asset.rb', line 7 def mtime @mtime end |
#write_path ⇒ Object
Returns the value of attribute write_path.
7 8 9 |
# File 'lib/spar/compiled_asset.rb', line 7 def write_path @write_path end |
Instance Method Details
#data ⇒ Object
52 53 54 |
# File 'lib/spar/compiled_asset.rb', line 52 def data @data ||= @asset ? @asset.to_s : @file.read end |
#generate_headers(options) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/spar/compiled_asset.rb', line 31 def generate_headers() headers = { :cache_control => [:cache_control] || Spar.settings['cache_control'], :acl => :public_read } if @write_path =~ /\/.html$/ headers[:content_type] = 'text/html; charset=utf-8' else headers[:content_type] = MIME::Types.of(@write_path).first end if @logical_path =~ /static\/downloads\// headers[:content_disposition] = "attachment; filename=#{File.basename(@write_path)}" end headers[:content_encoding] = :gzip if %w[svgz gz].index @write_path.split('.').last headers end |
#generate_write_path(options) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/spar/compiled_asset.rb', line 23 def generate_write_path() if [:digest] && @asset @asset.digest_path else @asset ? @logical_path : @logical_path.gsub(/^static\//,'') end end |
#write_to(base_path, options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/spar/compiled_asset.rb', line 56 def write_to(base_path, = {}) filename = File.join(base_path, @write_path) # Gzip contents if filename has '.gz' [:compress] ||= File.extname(filename) == '.gz' FileUtils.mkdir_p File.dirname(filename) File.open("#{filename}+", 'wb') do |f| if [:compress] # Run contents through `Zlib` gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION) gz.mtime = @mtime.to_i gz.write data gz.close else # Write out as is f.write data f.close end 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 |