Class: YUI::Compressor
- Inherits:
-
Object
- Object
- YUI::Compressor
- Defined in:
- lib/yui/compressor.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Error, OptionError, RuntimeError
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.compressor_type ⇒ Object
:nodoc:.
-
.default_options ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#compress(stream_or_string) ⇒ Object
Compress a stream or string of code with YUI Compressor.
-
#initialize(options = {}) ⇒ Compressor
constructor
:nodoc:.
Constructor Details
#initialize(options = {}) ⇒ Compressor
:nodoc:
20 21 22 23 |
# File 'lib/yui/compressor.rb', line 20 def initialize( = {}) #:nodoc: @options = self.class..merge() @command = [path_to_java, "-jar", path_to_jar_file, *(command_option_for_type + )] end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
10 11 12 |
# File 'lib/yui/compressor.rb', line 10 def command @command end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/yui/compressor.rb', line 10 def @options end |
Class Method Details
.compressor_type ⇒ Object
:nodoc:
16 17 18 |
# File 'lib/yui/compressor.rb', line 16 def self.compressor_type #:nodoc: raise Error, "create a CssCompressor or JavaScriptCompressor instead" end |
.default_options ⇒ Object
:nodoc:
12 13 14 |
# File 'lib/yui/compressor.rb', line 12 def self. #:nodoc: { :charset => "utf-8", :line_break => nil } end |
Instance Method Details
#compress(stream_or_string) ⇒ Object
Compress a stream or string of code with YUI Compressor. (A stream is any object that responds to read
and close
like an IO.) If a block is given, you can read the compressed code from the block’s argument. Otherwise, compress
returns a string of compressed code.
Example: Compress CSS
compressor = YUI::CssCompressor.new
compressor.compress(<<-END_CSS)
div.error {
color: red;
}
div.warning {
display: none;
}
END_CSS
# => "div.error{color:red;}div.warning{display:none;}"
Example: Compress JavaScript
compressor = YUI::JavaScriptCompressor.new
compressor.compress('(function () { var foo = {}; foo["bar"] = "baz"; })()')
# => "(function(){var foo={};foo.bar=\"baz\"})();"
Example: Compress and gzip a file on disk
File.open("my.js", "r") do |source|
Zlib::GzipWriter.open("my.js.gz", "w") do |gzip|
compressor.compress(source) do |compressed|
while buffer = compressed.read(4096)
gzip.write(buffer)
end
end
end
end
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/yui/compressor.rb', line 58 def compress(stream_or_string) streamify(stream_or_string) do |stream| Open3.popen3(*command) do |stdin, stdout, stderr| begin transfer(stream, stdin) if block_given? yield stdout else stdout.read end rescue Exception => e raise RuntimeError, "compression failed" end end end end |