Skip to content

Commit

Permalink
[Sass] Don't lazy-load configuration stuff under Rails.
Browse files Browse the repository at this point in the history
Closes sassgh-158
  • Loading branch information
nex3 committed May 11, 2010
1 parent 7d538d2 commit 1770f6d
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 177 deletions.
2 changes: 2 additions & 0 deletions doc-src/SASS_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* Make sure manually importing the Sass Rack plugin still works with Rails,
even though it's not necessary now.

* Allow Sass to be configured in Rails even when it's being lazy-loaded.

* Allow identifiers to begin with multiple underscores.

## 3.0.0
Expand Down
1 change: 1 addition & 0 deletions lib/haml/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
# Rails 3.0.0.beta.2+
if defined?(ActiveSupport) && Haml::Util.has?(:public_method, ActiveSupport, :on_load)
require 'haml/template/options'
require 'sass/plugin/configuration'
ActiveSupport.on_load(:action_view) {Haml.init_rails(binding)}
end
72 changes: 37 additions & 35 deletions lib/sass/callbacks.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
# A lightweight infrastructure for defining and running callbacks.
# Callbacks are defined using \{#define\_callback\} at the class level,
# and called using `run_#{name}` at the instance level.
#
# Clients can add callbacks by calling the generated `on_#{name}` method,
# and passing in a block that's run when the callback is activated.
#
# @example Define a callback
# class Munger
# extend Sass::Callbacks
# define_callback :string_munged
#
# def munge(str)
# res = str.gsub(/[a-z]/, '\1\1')
# run_string_munged str, res
# res
# end
# end
#
# @example Use a callback
# m = Munger.new
# m.on_string_munged {|str, res| puts "#{str} was munged into #{res}!"}
# m.munge "bar" #=> bar was munged into bbaarr!
module Sass::Callbacks
protected

# Define a callback with the given name.
# This will define an `on_#{name}` method
# that registers a block,
# and a `run_#{name}` method that runs that block
# (optionall with some arguments).
module Sass
# A lightweight infrastructure for defining and running callbacks.
# Callbacks are defined using \{#define\_callback\} at the class level,
# and called using `run_#{name}` at the instance level.
#
# Clients can add callbacks by calling the generated `on_#{name}` method,
# and passing in a block that's run when the callback is activated.
#
# @example Define a callback
# class Munger
# extend Sass::Callbacks
# define_callback :string_munged
#
# @param name [Symbol] The name of the callback
# @return [void]
def define_callback(name)
class_eval <<RUBY
# def munge(str)
# res = str.gsub(/[a-z]/, '\1\1')
# run_string_munged str, res
# res
# end
# end
#
# @example Use a callback
# m = Munger.new
# m.on_string_munged {|str, res| puts "#{str} was munged into #{res}!"}
# m.munge "bar" #=> bar was munged into bbaarr!
module Callbacks
protected

# Define a callback with the given name.
# This will define an `on_#{name}` method
# that registers a block,
# and a `run_#{name}` method that runs that block
# (optionall with some arguments).
#
# @param name [Symbol] The name of the callback
# @return [void]
def define_callback(name)
class_eval <<RUBY
def on_#{name}(&block)
@_sass_callbacks ||= {}
(@_sass_callbacks[#{name.inspect}] ||= []) << block
Expand All @@ -46,5 +47,6 @@ def run_#{name}(*args)
end
private :run_#{name}
RUBY
end
end
end
144 changes: 2 additions & 142 deletions lib/sass/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'rbconfig'

require 'sass'
require 'sass/callbacks'
require 'sass/plugin/configuration'
require 'sass/plugin/staleness_checker'

module Sass
Expand Down Expand Up @@ -31,155 +31,15 @@ module Sass
# #=> Compiling app/sass/ie.scss to public/stylesheets/ie.css
module Plugin
include Haml::Util
include Sass::Callbacks
extend self

@options = {
:css_location => './public/stylesheets',
:always_update => false,
:always_check => true,
:full_exception => true
}
@checked_for_updates = false

# Register a callback to be run before stylesheets are mass-updated.
# This is run whenever \{#update\_stylesheets} is called,
# unless the \{file:SASS_REFERENCE.md#never_update-option `:never_update` option}
# is enabled.
#
# @yield [individual_files]
# @yieldparam individual_files [<(String, String)>]
# Individual files to be updated, in addition to the directories
# specified in the options.
# The first element of each pair is the source file,
# the second is the target CSS file.
define_callback :updating_stylesheets

# Register a callback to be run before a single stylesheet is updated.
# The callback is only run if the stylesheet is guaranteed to be updated;
# if the CSS file is fresh, this won't be run.
#
# Even if the \{file:SASS_REFERENCE.md#full_exception-option `:full_exception` option}
# is enabled, this callback won't be run
# when an exception CSS file is being written.
# To run an action for those files, use \{#on\_compilation\_error}.
#
# @yield [template, css]
# @yieldparam template [String]
# The location of the Sass/SCSS file being updated.
# @yieldparam css [String]
# The location of the CSS file being generated.
define_callback :updating_stylesheet

# Register a callback to be run when Sass decides not to update a stylesheet.
# In particular, the callback is run when Sass finds that
# the template file and none of its dependencies
# have been modified since the last compilation.
#
# Note that this is **not** run when the
# \{file:SASS_REFERENCE.md#never-update_option `:never_update` option} is set,
# nor when Sass decides not to compile a partial.
#
# @yield [template, css]
# @yieldparam template [String]
# The location of the Sass/SCSS file not being updated.
# @yieldparam css [String]
# The location of the CSS file not being generated.
define_callback :not_updating_stylesheet

# Register a callback to be run when there's an error
# compiling a Sass file.
# This could include not only errors in the Sass document,
# but also errors accessing the file at all.
#
# @yield [error, template, css]
# @yieldparam error [Exception] The exception that was raised.
# @yieldparam template [String]
# The location of the Sass/SCSS file being updated.
# @yieldparam css [String]
# The location of the CSS file being generated.
define_callback :compilation_error

# Register a callback to be run when Sass creates a directory
# into which to put CSS files.
#
# Note that even if multiple levels of directories need to be created,
# the callback may only be run once.
# For example, if "foo/" exists and "foo/bar/baz/" needs to be created,
# this may only be run for "foo/bar/baz/".
# This is not a guarantee, however;
# it may also be run for "foo/bar/".
#
# @yield [dirname]
# @yieldparam dirname [String]
# The location of the directory that was created.
define_callback :creating_directory

# Register a callback to be run when Sass detects
# that a template has been modified.
# This is only run when using \{#watch}.
#
# @yield [template]
# @yieldparam template [String]
# The location of the template that was modified.
define_callback :template_modified

# Register a callback to be run when Sass detects
# that a new template has been created.
# This is only run when using \{#watch}.
#
# @yield [template]
# @yieldparam template [String]
# The location of the template that was created.
define_callback :template_created

# Register a callback to be run when Sass detects
# that a template has been deleted.
# This is only run when using \{#watch}.
#
# @yield [template]
# @yieldparam template [String]
# The location of the template that was deleted.
define_callback :template_deleted

# Register a callback to be run when Sass deletes a CSS file.
# This happens when the corresponding Sass/SCSS file has been deleted.
#
# @yield [filename]
# @yieldparam filename [String]
# The location of the CSS file that was deleted.
define_callback :deleting_css
@checked_for_updates = false

# Whether or not Sass has **ever** checked if the stylesheets need to be updated
# (in this Ruby instance).
#
# @return [Boolean]
attr_reader :checked_for_updates

# An options hash.
# See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
#
# @return [{Symbol => Object}]
attr_reader :options

# Sets the options hash.
# See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
#
# @param value [{Symbol => Object}] The options hash
def options=(value)
@options.merge!(value)
end

# Non-destructively modifies \{#options} so that default values are properly set.
#
# @param additional_options [{Symbol => Object}] An options hash with which to merge \{#options}
# @return [{Symbol => Object}] The modified options hash
def engine_options(additional_options = {})
opts = options.dup.merge(additional_options)
opts[:load_paths] = load_paths(opts)
opts
end

# Same as \{#update\_stylesheets}, but respects \{#checked\_for\_updates}
# and the {file:SASS_REFERENCE.md#always_update-option `:always_update`}
# and {file:SASS_REFERENCE.md#always_check-option `:always_check`} options.
Expand Down
Loading

0 comments on commit 1770f6d

Please sign in to comment.