Module: YUICompressor::JRuby

Defined in:
lib/yuicompressor/jruby.rb

Overview

This module contains methods specific to the JRuby platform and is automatically used when JRuby is detected. It provides a significant increase in performance over the Shell module.

Defined Under Namespace

Classes: ErrorReporter

Instance Method Summary collapse

Instance Method Details

#command_arguments(options = {}) ⇒ Object

Returns the set of arguments that are needed to instantiate a compressor using the given options.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yuicompressor/jruby.rb', line 32

def command_arguments(options={})
  args = [ options[:line_break] ? options[:line_break].to_i : -1 ]

  if options[:type].to_s == 'js'
    args << !! options[:munge]
    args << false # verbose?
    args << !! options[:preserve_semicolons]
    args << ! options[:optimize] # disable optimizations?
  end

  args
end

#compress(stream_or_string, options = {}) ⇒ Object

Compresses the given stream_or_string of code using the given options. When using this method directly, at least the :type option must be specified, and should be one of "css" or "js". See YUICompressor#compress_css and YUICompressor#compress_js for details about which options are acceptable for each type of compressor.

If a block is given, it will receive the IO output object. Otherwise the output will be returned as a string.

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/yuicompressor/jruby.rb', line 53

def compress(stream_or_string, options={})
  raise ArgumentError, 'Option :type required' unless options.key?(:type)

  stream = streamify(stream_or_string)
  output = StringIO.new

  reader = InputStreamReader.new(stream.to_inputstream)
  writer = OutputStreamWriter.new(output.to_outputstream)

  compressor = case options[:type].to_s
  when 'js'
    options = default_js_options.merge(options)
    JavaScriptCompressor.new(reader, ErrorReporter.new)
  when 'css'
    options = default_css_options.merge(options)
    CssCompressor.new(reader)
  else
    raise ArgumentError, 'Unknown resource type: %s' % options[:type]
  end

  compressor.compress(writer, *command_arguments(options))
  writer.flush
  output.rewind

  if block_given?
    yield output
  else
    output.read
  end
end