Class: Closure::Compiler::Compilation
- Inherits:
-
Object
- Object
- Closure::Compiler::Compilation
- Defined in:
- lib/closure/compiler.rb
Instance Attribute Summary collapse
-
#js_output_file ⇒ Object
readonly
Returns the value of attribute js_output_file.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
Instance Method Summary collapse
-
#<<(javascript) ⇒ Object
Appends a string to the javascript.
-
#initialize(env, js_output_file = nil, log = nil) ⇒ Compilation
constructor
A new instance of Compilation.
-
#javascript ⇒ Object
(also: #to_s)
Always returns the compiled javascript (possibly an empty string).
-
#to_response ⇒ Rack::Response
Turn the compiled javascript into a Rack::Response object.
- #to_response_with_console ⇒ Object
Constructor Details
#initialize(env, js_output_file = nil, log = nil) ⇒ Compilation
Returns a new instance of Compilation.
97 98 99 100 101 102 |
# File 'lib/closure/compiler.rb', line 97 def initialize(env, js_output_file=nil, log=nil) @javascript = [] @env = env @js_output_file = js_output_file @log = log end |
Instance Attribute Details
#js_output_file ⇒ Object (readonly)
Returns the value of attribute js_output_file.
95 96 97 |
# File 'lib/closure/compiler.rb', line 95 def js_output_file @js_output_file end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
94 95 96 |
# File 'lib/closure/compiler.rb', line 94 def log @log end |
Instance Method Details
#<<(javascript) ⇒ Object
Appends a string to the javascript.
142 143 144 145 |
# File 'lib/closure/compiler.rb', line 142 def <<(javascript) @javascript << javascript self end |
#javascript ⇒ Object Also known as: to_s
Always returns the compiled javascript (possibly an empty string).
150 151 152 153 154 155 156 157 |
# File 'lib/closure/compiler.rb', line 150 def javascript if @js_output_file file_js = File.read(@js_output_file) rescue '' ([file_js] + @javascript).join nil else @javascript.join nil end end |
#to_response ⇒ Rack::Response
Turn the compiled javascript into a Rack::Response object. Success and warning messages, which aren’t raised like errors, will be logged to the javascript console.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/closure/compiler.rb', line 120 def to_response if !log and @javascript.empty? and @js_output_file response = FileResponse.new @env, @js_output_file, 'application/javascript' else response = Rack::Response.new response.headers['Content-Type'] = 'application/javascript' response.headers["Cache-Control"] = 'max-age=0, private, must-revalidate' if log consolable_log = '"Closure Compiler: %s\n\n", ' + log.rstrip.dump if log.split("\n").first =~ / 0 warn/i response.write "try{console.log(#{consolable_log})}catch(err){};\n" else response.write "try{console.warn(#{consolable_log})}catch(err){};\n" end end response.write javascript end response end |
#to_response_with_console ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/closure/compiler.rb', line 105 def to_response_with_console response = to_response if response.class == Rack::Response msg = "#to_response_with_console deprecated, use #to_response" response.write "try{console.warn(#{msg.dump})}catch(err){};\n" end response end |