Class: Sass::Plugin::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/sass/plugin/rack.rb

Overview

Rack middleware for compiling Sass code.

Activate

require 'sass/plugin/rack'
use Sass::Plugin::Rack

Customize

Sass::Plugin.options.merge(
  :cache_location => './tmp/sass-cache',
  :never_update => environment != :production,
  :full_exception => environment != :production)

See the Reference for more options.

Use

Put your Sass files in public/stylesheets/sass. Your CSS will be generated in public/stylesheets, and regenerated every request if necessary. The locations and frequency can be customized. That's all there is to it!

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack

Initialize the middleware.

Parameters:

  • app (#call)

    The Rack application



32
33
34
35
# File 'lib/sass/plugin/rack.rb', line 32

def initialize(app)
  @app = app
  self.class.disable_native_plugin!
end

Class Method Details

.disable_native_plugin!

Disable the native Rails or Merb plugins, if they're enabled. This is automatically done once the Rack plugin is activated. This is done so that the stylesheets aren't checked twice for each request.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sass/plugin/rack.rb', line 50

def self.disable_native_plugin!
  if defined?(Merb::Rack) && defined?(Merb::Rack::Application) &&
      Haml::Util.has?(:instance_method, Merb::Rack::Application, :call_without_sass)
    Merb::Rack::Application.instance_eval {alias_method :call, :call_without_sass}
  end

  if defined?(ActionDispatch::Callbacks) && defined?(ActionDispatch::Callbacks.to_prepare)
    ActionDispatch::Callbacks.skip_callback(:prepare, :__sass_process)
  elsif defined?(ActionController::Base) &&
      Haml::Util.has?(:instance_method, ActionController::Base, :sass_old_process)
    ActionController::Base.instance_eval {alias_method :process, :sass_old_process}
  end
end

Instance Method Details

#call(env) ⇒ (#to_i, {String => String}, Object)

Process a request, checking the Sass stylesheets for changes and updating them if necessary.

Parameters:

  • env

    The Rack request environment

Returns:

  • ((#to_i, {String => String}, Object))

    The Rack response



42
43
44
45
# File 'lib/sass/plugin/rack.rb', line 42

def call(env)
  Sass::Plugin.check_for_updates
  @app.call(env)
end