Class: Closure::Compiler::Compilation

Inherits:
Object
  • Object
show all
Defined in:
lib/closure/compiler.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Compilation) initialize(env, js_output_file = nil, log = nil)

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 (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 (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)

Appends a string to the javascript.

Parameters:

  • javascript (String)


142
143
144
145
# File 'lib/closure/compiler.rb', line 142

def <<(javascript)
  @javascript << javascript
  self
end

- javascript Also known as: to_s

Always returns the compiled javascript (possibly an empty string).

Examples:

<%= goog.compile(args) %>


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

- (Rack::Response) to_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.

Examples:

<% @response = goog.compile(args).to_response %>

Returns:

  • (Rack::Response)


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