Class: Rack::CoffeeCompiler

Inherits:
AssetCompiler show all
Defined in:
lib/rack/coffee_compiler.rb

Constant Summary collapse

LOCK =
Mutex.new

Constants inherited from AssetCompiler

AssetCompiler::F

Instance Attribute Summary

Attributes inherited from AssetCompiler

#source_dir, #source_extension, #url

Instance Method Summary collapse

Methods inherited from AssetCompiler

#call, #response

Constructor Details

#initialize(app, options = {}) ⇒ CoffeeCompiler

Returns a new instance of CoffeeCompiler.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rack/coffee_compiler.rb', line 8

def initialize(app, options={})
  options = {
    :url => '/javascripts',
    :content_type => 'text/javascript',
    :source_extension => 'coffee',
    :alert_on_error => ENV['RACK_ENV'] != 'production',
    :lock => LOCK
  }.merge(options)

  @alert_on_error = options[:alert_on_error]
  @lock = options[:lock]
  super
end

Instance Method Details

#compile(source_file) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rack/coffee_compiler.rb', line 22

def compile(source_file)
  if @lock
    @lock.synchronize{ unsynchronized_compile(source_file) }
  else
    unsynchronized_compile(source_file)
  end
end

#unsynchronized_compile(source_file) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rack/coffee_compiler.rb', line 30

def unsynchronized_compile(source_file)
  begin
    CoffeeScript.compile(::File.read(source_file))
  rescue CoffeeScript::CompilationError => e
    if @alert_on_error
      error_msg = "CoffeeScript compilation error in #{source_file}.coffee:\n\n #{e.to_s}"
      "window.alert(#{error_msg.to_json});"
    else
      raise e
    end
  end
end