Sha256: 7cc93257d1f2f7ed6056481bfd96f47359a115d79ab7b550815ebd4e416037c5

Contents?: true

Size: 1.83 KB

Versions: 21

Compression:

Stored size: 1.83 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/less"
    require "#{AssetPack::PREFIX}/assetpack/engines/sqwish"
    require "#{AssetPack::PREFIX}/assetpack/engines/closure"
    require "#{AssetPack::PREFIX}/assetpack/engines/uglify"
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
sinatra-assetpack-0.3.3 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.3.2 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.3.1 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.3.0 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.2.8 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.2.7 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.2.6 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.2.5 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.2.4 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.2.3 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.2.2 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.2.1 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.2.0 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.1.7 lib/sinatra/assetpack/compressor.rb
sundawg-sinatra-assetpack-fork-0.1.6 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.1.6 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.1.5 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.1.4 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.1.3 lib/sinatra/assetpack/compressor.rb
sinatra-assetpack-0.1.2 lib/sinatra/assetpack/compressor.rb