Class: Cjoiner::Engines::Compressor

Inherits:
Engine
  • Object
show all
Defined in:
lib/cjoiner/engines/compressor.rb

Overview

compress content

Instance Attribute Summary

Attributes inherited from Engine

#engine

Instance Method Summary collapse

Methods inherited from Engine

#render

Methods included from Helpers::Files

#delete_file, #expand_path, #file, #file_exists, #load_yaml, #move_file, #on_windows, #read_file, #temp_file, #write_file

Constructor Details

#initialize(opts) ⇒ Compressor

Returns a new instance of Compressor.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cjoiner/engines/compressor.rb', line 7

def initialize(opts)
  # use the standalone java jar file
  if opts[:yui]
    temp = temp_file "cjoiner.#{opts[:type]}", opts[:content]
    munge = !opts[:munge] ? "--nomunge" : ""
    @engine = `java -jar #{opts[:yui]} #{munge} --charset #{opts[:charset]} --type #{opts[:type]} #{temp.path}` if file_exists opts[:yui]
    delete_file temp
  else
    case opts[:type]
      when :css
        compressor = ::YUI::CssCompressor.new(:charset => opts[:charset])
      when :js
        compressor = ::YUI::JavaScriptCompressor.new(:munge => opts[:munge], :charset => opts[:charset])
    end
    if compressor
      @engine = compressor.compress(opts[:content])
    else
      @engine = opts[:content]
    end
  end
end