Sha256: ee79e6c812e28502ef1e8f6390194372bdbb2b751de35960fc8f1270e3ef6c29

Contents?: true

Size: 1.77 KB

Versions: 6

Compression:

Stored size: 1.77 KB

Contents

module Sinatra
  module AssetPack
    module Compressor
      extend self

      # Compresses a given string.
      #
      #     compress File.read('x.js'), :js, :jsmin
      #
      def compress(str, type, engine=nil, options={})
        # Use defaults if no engine is given.
        return fallback(str, type, options)  if engine.nil?

        # Ensure that the engine exists.
        klass = compressors[[type, engine]]
        raise Error, "Engine #{engine} (#{type}) doesn't exist."  unless klass

        # Ensure that the engine can support that type.
        engine = klass.new
        raise Error, "#{klass} does not support #{type.upcase} compression."  unless engine.respond_to?(type)

        # Build it using the engine, and fallback to defaults if it fails.
        output   = engine.send type, str, options
        output ||= fallback(str, type, options)  unless options[:no_fallback]
        output
      end

      # Compresses a given string using the default engines.
      def fallback(str, type, options)
        if type == :js
          compress str, :js, :jsmin, :no_fallback => true
        elsif type == :css
          compress str, :css, :simple, :no_fallback => true
        end
      end

      def compressors
        @compressors ||= Hash.new
      end

      def register(type, engine, meth)
        compressors[[type, engine]] = meth
      end
    end

    require "#{AssetPack::PREFIX}/assetpack/engines/simple"
    require "#{AssetPack::PREFIX}/assetpack/engines/jsmin"
    require "#{AssetPack::PREFIX}/assetpack/engines/yui"
    require "#{AssetPack::PREFIX}/assetpack/engines/sass"
    require "#{AssetPack::PREFIX}/assetpack/engines/sqwish"
    require "#{AssetPack::PREFIX}/assetpack/engines/closure"
    require "#{AssetPack::PREFIX}/assetpack/engines/uglify"
  end
end

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
sinatra-assetpack-0.1.0 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-flexible-compression-0.0.1 lib/sinatra/assetpack/compressor.rb
sundawg-sinatra-assetpack-fork-0.0.12.pre1 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.0.12.pre1 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.0.11 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.0.10 lib/sinatra/assetpack/compressor.rb