Class: Spar::CompiledAsset

Inherits:
Object
  • Object
show all
Defined in:
lib/spar/compiled_asset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  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(options)
  @headers    = generate_headers(options)
  @mtime      = (@asset || @file).mtime
  @digest     = @asset ? @asset.digest : Digest::MD5.hexdigest(data)
end

Instance Attribute Details

#assetObject

Returns the value of attribute asset.



7
8
9
# File 'lib/spar/compiled_asset.rb', line 7

def asset
  @asset
end

#digestObject

Returns the value of attribute digest.



7
8
9
# File 'lib/spar/compiled_asset.rb', line 7

def digest
  @digest
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/spar/compiled_asset.rb', line 7

def headers
  @headers
end

#logical_pathObject

Returns the value of attribute logical_path.



7
8
9
# File 'lib/spar/compiled_asset.rb', line 7

def logical_path
  @logical_path
end

#mtimeObject

Returns the value of attribute mtime.



7
8
9
# File 'lib/spar/compiled_asset.rb', line 7

def mtime
  @mtime
end

#write_pathObject

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

#dataObject



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(options)
  headers = {
    :cache_control => options[: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(options)
  if options[: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, options = {})
  filename = File.join(base_path, @write_path)
  # Gzip contents if filename has '.gz'
  options[:compress] ||= File.extname(filename) == '.gz'

  FileUtils.mkdir_p File.dirname(filename)

  File.open("#{filename}+", 'wb') do |f|
    if options[: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