Class: AssetResource::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/asset_resource/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/asset_resource/middleware.rb', line 7

def initialize(app, options={})
  @app = app
  @options = options

  if options[:handlers] then
    options[:handlers].each do |handler, mime_type|
      handle handler.to_sym, mime_type
    end
  else
    handle :scripts, "text/javascript"
    handle :styles,  "text/css"
  end

  translator :css, &passthrough_translator
  translator :js,  &passthrough_translator

  translator :less do |filename|
    begin
      require "less"
      Less::Engine.new(File.read(filename)).to_css
    rescue LoadError
      raise "Tried to translate a less file but could not find the library.\nTry adding this to your Gemfile:\n  gem \"less\""
    end
  end

  translator :sass do |filename|
    begin
      require "sass"
      Sass::Engine.new(File.read(filename), :load_paths => [File.dirname(filename)]).render
    rescue LoadError
      raise "Tried to translate a sass file but could not find the library.\nTry adding this to your Gemfile:\n  gem \"haml\""
    end
  end
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



5
6
7
# File 'lib/asset_resource/middleware.rb', line 5

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/asset_resource/middleware.rb', line 5

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/asset_resource/middleware.rb', line 42

def call(env)
  if env["PATH_INFO"] =~ %r{\A/assets/(.+)\B}
    type = $1.split(".").first
    return [200, asset_headers(type), process_files(files_for(type))] if handles?(type)
  end
  app.call(env)
end