Sha256: 84ac8ccdbca8224a7f8952164be39de8649bee2f2b8f72b63b973e6c5cc0c41d

Contents?: true

Size: 1.02 KB

Versions: 15

Compression:

Stored size: 1.02 KB

Contents

# encoding: UTF-8

require 'coffee-script'

module Spontaneous::Rack
  class JS
    JS_EXTENSION = /\.js$/o

    def initialize(app, options = {})
      @roots = options[:root]
      @app = app
    end

    def call(env)
      try_coffeescript(env[S::PATH_INFO]) or @app.call(env)
    end

    def try_coffeescript(request_path)
      return nil unless request_path =~ JS_EXTENSION
      try_paths = @roots.map { |root| File.join(root, request_path) }
      # if the css file itself exists then we want to use that
      return nil if try_paths.detect { |file| ::File.exists?(file) }

      try_paths.each do |path|
        template = template_path(path)
        return render_coffeescript(template) if File.exists?(template)
      end
      nil
    end

    def render_coffeescript(template)
      script = CoffeeScript.compile(File.read(template))
      [200, {'Content-type' => 'application/javascript;charset=utf-8'}, StringIO.new(script)]
    end

    def template_path(path)
      path.gsub(JS_EXTENSION, ".coffee")
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
spontaneous-0.2.0.beta10 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.beta9 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.beta8 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.beta7 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.beta6 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.beta5 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.beta4 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.beta3 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.beta2 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.beta1 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.alpha7 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.alpha6 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.alpha5 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.alpha4 lib/spontaneous/rack/js.rb
spontaneous-0.2.0.alpha3 lib/spontaneous/rack/js.rb