123456789_123456789_123456789_123456789_123456789_

Class: Sprockets::Utils::Gzip

Relationships & Source Files
Namespace Children
Modules:
Inherits: Object
Defined in: lib/sprockets/utils/gzip.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(asset, archiver: ZlibArchiver) ⇒ Gzip

Private: Generates a gzipped file based off of reference file.

[ GitHub ]

  
# File 'lib/sprockets/utils/gzip.rb', line 43

def initialize(asset, archiver: ZlibArchiver)
  @content_type  = asset.content_type
  @source        = asset.source
  @charset       = asset.charset
  @archiver      = archiver
end

Instance Attribute Details

#archiver (readonly)

[ GitHub ]

  
# File 'lib/sprockets/utils/gzip.rb', line 40

attr_reader :content_type, :source, :charset, :archiver

#can_compress?Boolean (readonly)

Private: Returns whether or not an asset can be compressed.

We want to compress any file that is text based. You do not want to compress binary files as they may already be compressed and running them through a compression algorithm would make them larger.

Return Boolean.

[ GitHub ]

  
# File 'lib/sprockets/utils/gzip.rb', line 68

def can_compress?
  # The "charset" of a mime type is present if the value is
  # encoded text. We can check this value to see if the asset
  # can be compressed.
  #
  # We also check against our list of non-text compressible mime types
  @charset || COMPRESSABLE_MIME_TYPES.include?(@content_type)
end

#cannot_compress?Boolean (readonly)

Private: Opposite of #can_compress?.

Returns Boolean.

[ GitHub ]

  
# File 'lib/sprockets/utils/gzip.rb', line 80

def cannot_compress?
  !can_compress?
end

#charset (readonly)

[ GitHub ]

  
# File 'lib/sprockets/utils/gzip.rb', line 40

attr_reader :content_type, :source, :charset, :archiver

#content_type (readonly)

[ GitHub ]

  
# File 'lib/sprockets/utils/gzip.rb', line 40

attr_reader :content_type, :source, :charset, :archiver

#source (readonly)

[ GitHub ]

  
# File 'lib/sprockets/utils/gzip.rb', line 40

attr_reader :content_type, :source, :charset, :archiver

Instance Method Details

#compress(file, target)

Private: Generates a gzipped file based off of reference asset.

Compresses the target asset’s contents and puts it into a file with the same name plus a .gz extension in the same folder as the original. Does not modify the target asset.

Returns nothing.

[ GitHub ]

  
# File 'lib/sprockets/utils/gzip.rb', line 91

def compress(file, target)
  mtime = Sprockets::PathUtils.stat(target).mtime
  archiver.call(file, source, mtime)

  nil
end