Thanks leucos !!!
I though about a Rack middleware too but wonder if there wasn't a more "ramaze" solution (like with innate).
Anyway, this works and it's great !
Here is a modified version of your code :
require 'coffee-script'
# An attempt to allow Ramaze to generate Js file from coffee files
module Rack
class BrewedCoffee
def initialize(app, options = {})
@app = app
@lookup_path = options[:lookup_path].nil? ? [__DIR__] : options[:lookup_path]
end
def call(env)
name = env['PATH_INFO']
# Continue processing if requested file is not js
return @app.call(env) if File.extname(name) != '.js'
coffee_name = "#{name[0...-3]}.coffee"
@lookup_path.each do |p|
coffee_file = File.join(p, coffee_name)
next unless File.file?(coffee_file)
response_body = CoffeeScript.compile(File.read(coffee_file))
headers = {}
headers["Content-Type"] = 'application/javascript'
headers["Content-Length"] = response_body.length.to_s
return [200, headers, [response_body]]
end
@app.call(env)
end
end
end
I clean things a bit and remove the Ramaze dependency, this way it's a pure Rack middleware :).
You can use it calling :
m.use Rack::BrewedCoffee, :lookup_path => Ramaze.options.roots
Bye,
Andreas