Sha256: ab77c80f1954a03e19146c8d0266958a9b5b0e5aa755715c86c5d43105d9333e

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module Rail
  class Pipeline
    extend Forwardable

    def_delegators :@host, :root, :gems, :helpers, :compress?

    def initialize(host)
      @host = host
    end

    def process(request)
      context = Context.new(locals: { request: request }, mixins: helpers)

      asset = rewrite(request.path)
      klass = Processor.find(asset) or raise NotFoundError
      asset = klass.extensify(asset)
      filename = find(asset) or raise NotFoundError

      processor = klass.new(self)
      body = processor.compile(filename, context: context)

      headers = { 'Content-Type' => klass.mime_type }

      [ 200, headers, Array(body) ]
    rescue NotFoundError
      [ 404, {}, [] ]
    end

    def find(asset)
      paths.each do |path|
        filename = File.join(path, asset)
        return filename if File.exist?(filename)
      end
      nil
    end

    private

    def rewrite(path)
      if [ '', 'index.html' ].include?(path)
        'layouts/application.html'
      elsif File.extname(path).empty?
        "#{ path }.html"
      else
        path
      end
    end

    def paths
      @paths ||= (application_paths + gems.map { |gem| gem_paths(gem) }).flatten
    end

    def application_paths
      [
        File.join(root, 'app/assets/javascripts'),
        File.join(root, 'app/assets/stylesheets'),
        File.join(root, 'app/views')
      ]
    end

    def gem_paths(name)
      gem = Gem::Specification.find_by_name(name)
      [
        File.join(gem.gem_dir, 'lib/assets/javascripts'),
        File.join(gem.gem_dir, 'lib/assets/stylesheets')
      ]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rail-0.0.7 lib/rail/pipeline.rb
rail-0.0.6 lib/rail/pipeline.rb