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.
-
#compiled? ⇒ Boolean
True when compilation actually happened.
-
#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.
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.
138 139 140 141 |
# File 'lib/closure/compiler.rb', line 138 def <<(javascript) @javascript << javascript self end |
#compiled? ⇒ Boolean
True when compilation actually happened. False when build was up to date.
106 107 108 |
# File 'lib/closure/compiler.rb', line 106 def compiled? !!@log end |
#javascript ⇒ Object Also known as: to_s
Always returns the compiled javascript (possibly an empty string).
146 147 148 149 150 151 152 153 |
# File 'lib/closure/compiler.rb', line 146 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.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/closure/compiler.rb', line 116 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 |