Permalink
Browse files

CoffeeCompiler now works

  • Loading branch information...
1 parent 2e2d1f1 commit a639039337ef934d2dfbb891a6d92c364c82fde8 @jbaudanza jbaudanza committed Feb 14, 2011
@@ -43,8 +43,8 @@ def call(env)
return response( 403, 'Forbidden') if request_path.include? ".."
urls.each do |url|
- match_parts = url.split('/')
- request_parts = request_path.split('/')
+ match_parts = url.split('/').select{ |str| str.length > 0 }
+ request_parts = request_path.split('/').select{ |str| str.length > 0 }
if(request_parts.take(match_parts.length) == match_parts)
request_base = request_parts[match_parts.length..-1]
@@ -0,0 +1,27 @@
+require 'rack/almost_static'
+require 'coffee-script'
+
+module Rack
+ class CoffeeCompiler < AlmostStatic
+ def initialize(app, options={})
+ options[:urls] ||= '/javascripts'
+ options[:content_type] ||= 'text/javascript'
+ options[:source_extension] ||= 'coffeescript'
+ @alert_on_error = options.has_key?(:alert_on_error) ? options[:alert_on_error] : ENV['RACK_ENV'] != 'production'
+ super
+ end
+
+ def 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
+ end
+end
@@ -12,11 +12,10 @@
'chickenscript'
end
- @source_dir = File.join(File.dirname(__FILE__), 'eggscript')
+ @source_dir = File.join(File.dirname(__FILE__), 'fixtures/eggscripts')
@options = {
:urls => '/chickenscripts/',
- :root => 'eggscripts',
:source_dir => @source_dir,
:source_extension => 'eggscript',
:content_type => 'text/chicken-script',
@@ -0,0 +1,59 @@
+require 'rack/coffee_compiler'
+require 'rack/lobster'
+require "rack/test"
+
+include Rack::Test::Methods
+
+describe "CoffeeCompiler" do
+ before do
+ @source_dir = File.join(File.dirname(__FILE__), 'fixtures/coffeescripts')
+
+ @options = {
+ :source_dir => @source_dir
+ }
+ end
+
+ def app
+ options = @options
+ Rack::Builder.new {
+ use Rack::CoffeeCompiler, options
+ run Rack::Lobster.new
+ }
+ end
+
+ it "should compile the coffeescript" do
+ get '/javascripts/application.js'
+ last_response.body.should == CoffeeScript.compile(File.read("#{@source_dir}/application.coffeescript"))
+ last_response.status.should == 200
+ last_response.content_type.should == 'text/javascript'
+ end
+
+ describe "when compiling a file with a syntax error" do
+ def do_get
+ get '/javascripts/syntax_error.js'
+ end
+
+ it "should render a window.alert by default" do
+ @options.delete(:alert_on_error)
+ do_get
+ last_response.body.should include('window.alert')
+ end
+
+ it "should not render a window.alert by default on production" do
+ @options.delete(:alert_on_error)
+ ENV['RACK_ENV'] = 'production'
+ lambda { do_get }.should raise_error(CoffeeScript::CompilationError)
+ end
+
+ it "should not render a window.alert if alert_on_error is false" do
+ @options[:alert_on_error] = false
+ lambda { do_get }.should raise_error(CoffeeScript::CompilationError)
+ end
+
+ it "should render a window.alert if alert_on_error is true" do
+ @options[:alert_on_error] = true
+ do_get
+ last_response.body.should include('window.alert')
+ end
+ end
+end
@@ -0,0 +1 @@
+console.log('hello world')
@@ -0,0 +1 @@
+almost_called_a_function(1,2

0 comments on commit a639039

Please sign in to comment.