aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/sprockets/compressors.rb
blob: 351eff10855a64b4bcb789c064ec563d8a507691 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Sprockets
  # An asset compressor which does nothing.
  #
  # This compressor simply returns the asset as-is, without any compression
  # whatsoever. It is useful in development mode, when compression isn't
  # needed but using the same asset pipeline as production is desired.
  class NullCompressor #:nodoc:
    def compress(content)
      content
    end
  end

  # An asset compressor which only initializes the underlying compression
  # engine when needed.
  #
  # This postpones the initialization of the compressor until
  # <code>#compress</code> is called the first time.
  class LazyCompressor #:nodoc:
    # Initializes a new LazyCompressor.
    #
    # The block should return a compressor when called, i.e. an object
    # which responds to <code>#compress</code>.
    def initialize(&block)
      @block = block
    end

    def compress(content)
      compressor.compress(content)
    end

    private

    def compressor
      @compressor ||= (@block.call || NullCompressor.new)
    end
  end
end