Sha256: ecedbcb2931f8c88d1649d8b1df70973c83fb69ddcb5c1546ad1435d93c66652

Contents?: true

Size: 1.88 KB

Versions: 12

Compression:

Stored size: 1.88 KB

Contents

require "rack/asset_compiler"
require "rack/sass_compiler"

module Rack
  class HalfPipe

    def initialize(app)
      @compilers = []

      @compilers << Rack::AssetCompiler.new(app, scripts_config)
      @compilers << Rack::AssetCompiler.new(app, bower_js_config)
      @compilers << Rack::AssetCompiler.new(app, bower_css_config)
      @compilers << Rack::AssetCompiler.new(app, images_config)
      @compilers << Rack::SassCompiler.new(app, sass_config)

      @app = app
    end

    def call(env)

      res = @compilers.reduce([404]) do |memo,compiler|
        if memo[0] == 404
          compiler.call(env)
        else
          memo
        end
      end

      if res[0] == 404
        @app.call(env)
      else
        res
      end

    end

    private

    def scripts_config
      {
        source_dir: 'app/scripts',
        url: '/scripts',
        source_extension: 'js',
        content_type: 'application/javascript',
        compiler: pass_thru_compiler
      }
    end

    def bower_js_config
      {
        source_dir: 'bower_components',
        url: '/components',
        source_extension: 'js',
        content_type: 'application/javascript',
        compiler: pass_thru_compiler
      }
    end

    def images_config
      {
        source_dir: 'app/images',
        url: '/images',
        compiler: pass_thru_compiler
      }
    end

    def bower_css_config
      {
        source_dir: 'components',
        url: '/components/css',
        source_extension: 'css',
        content_type: 'text/css',
        compiler: pass_thru_compiler
      }
    end

    def sass_config
      {
        :source_dir => 'app/styles',
        :url => '/styles',
        sass_options: {
          debug_info: true,
          line_comments: false,
          style: :expanded
        }
      }
    end

    def pass_thru_compiler
      ->(source_file){
        ::File.read(source_file)
      }
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
half-pipe-0.3.0.beta.2 lib/rack/half-pipe.rb
half-pipe-0.3.0.beta.1 lib/rack/half-pipe.rb
half-pipe-0.3.0.alpha.5 lib/rack/half-pipe.rb
half-pipe-0.3.0.alpha.4 lib/rack/half-pipe.rb
half-pipe-0.3.0.alpha.3 lib/rack/half-pipe.rb
half-pipe-0.3.0.alpha.2 lib/rack/half-pipe.rb
half-pipe-0.3.0.alpha.1 lib/rack/half-pipe.rb
half-pipe-0.2.3 lib/rack/half-pipe.rb
half-pipe-0.2.2 lib/rack/half-pipe.rb
half-pipe-0.2.1 lib/rack/half-pipe.rb
half-pipe-0.2.0 lib/rack/half-pipe.rb
half-pipe-0.1.0 lib/rack/half-pipe.rb