Class: Rack::CssDryer

Inherits:
Object show all
Includes:
CssDryer::Processor
Defined in:
lib/rack/css_dryer.rb

Constant Summary collapse

File =
::File

Constants included from CssDryer::Processor

CssDryer::Processor::VERSION

Instance Method Summary collapse

Methods included from CssDryer::Processor

#nested_css_to_structure, #process, #structure_to_css

Constructor Details

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

Options:

:url the parent URL of the stylesheets. Defaults to ‘css’. In a Rails app you probably want to set this to ‘stylesheets’.

:path the file system directory containing your nested stylesheets (.ncss). In a Rails app you probably want to set this to ‘app/views/stylesheets’.



21
22
23
24
25
# File 'lib/rack/css_dryer.rb', line 21

def initialize(app, options = {})
  @app  = app
  @url  = options[:url]  || 'css'
  @path = options[:path] || 'stylesheets'
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rack/css_dryer.rb', line 27

def call(env)
  path = env['PATH_INFO']
  if path =~ %r{/#{@url}/}
    file = path.sub("/#{@url}", @path)
    ncss = file.sub(/\.css$/, '.ncss')

    if File.exists?(ncss)
      nested_css = File.read(ncss)
      nested_css = ::ERB.new(nested_css, nil, '-').result
      css = process(nested_css)
      length = ''.respond_to?(:bytesize) ? css.bytesize.to_s : css.size.to_s
      [200, {'Content-Type' => 'text/css', 'Content-Length' => length}, [css]]
    else
      @app.call(env)
    end
  else
    @app.call(env)
  end
end