Move callbacks to the configuration base class and make them work correctly with inherited data.

This commit is contained in:
Chris Eppstein 2011-08-28 12:58:08 -07:00
parent 5896711c8a
commit e2960d81b0
6 changed files with 45 additions and 38 deletions

View File

@ -138,7 +138,7 @@ module Compass
end
duration = options[:time] ? "(#{(css_content.__duration * 1000).round / 1000.0}s)" : ""
write_file(css_filename, css_content, options.merge(:force => true, :extra => duration))
Compass.configuration.run_callback(:stylesheet_saved, css_filename)
Compass.configuration.run_stylesheet_saved(css_filename)
end
def should_compile?(sass_filename, css_filename)
@ -159,7 +159,7 @@ module Compass
formatted_error = "(Line #{e.sass_line}: #{e.message})"
file = basename(sass_filename)
logger.record :error, file, formatted_error
Compass.configuration.run_callback(:stylesheet_error, sass_filename, formatted_error)
Compass.configuration.run_stylesheet_error(sass_filename, formatted_error)
write_file css_filename, error_contents(e, sass_filename), options.merge(:force => true)
end

View File

@ -18,12 +18,38 @@ module Compass
class Data
attr_reader :name
extend Sass::Callbacks
include Compass::Configuration::Inheritance
include Compass::Configuration::Serialization
include Compass::Configuration::Adapters
extend Compass::Configuration::Paths
# on_sprite_saved
# yields the filename
# usage: on_sprite_saved {|filename| do_something(filename) }
define_callback :sprite_saved
chained_method :run_sprite_saved
# on_sprite_generated
# yields 'ChunkyPNG::Image'
# usage: on_sprite_generated {|sprite_data| do_something(sprite_data) }
define_callback :sprite_generated
chained_method :run_sprite_generated
# on_stylesheet_saved
# yields the filename
# usage: on_stylesheet_saved {|filename| do_something(filename) }
define_callback :stylesheet_saved
chained_method :run_stylesheet_saved
# on_stylesheet_error
# yields the filename & message
# usage: on_stylesheet_error {|filename, message| do_something(filename, message) }
define_callback :stylesheet_error
chained_method :run_stylesheet_error
inherited_accessor *ATTRIBUTES
inherited_accessor :required_libraries, :loaded_frameworks, :framework_path #XXX we should make these arrays add up cumulatively.
@ -138,16 +164,6 @@ module Compass
relative_assets || http_images_path == :relative
end
def run_callback(event, *args)
begin
send(:"run_#{event}", *args)
rescue NoMethodError => e
unless e.message =~ /run_#{event}/
raise
end
end
end
private
def assert_valid_keys!(attr_hash)

View File

@ -1,28 +1,6 @@
module Compass
module Configuration
class FileData < Data
extend Sass::Callbacks
# on_sprite_generated
# yields the filename
# usage: on_sprite_save {|filename| do_somethign(filename) }
define_callback :sprite_saved
# on_sprite_generated
# yields 'ChunkyPNG::Image'
# usage: on_sprite_generated {|sprite_data| do_something(sprite_data) }
define_callback :sprite_generated
# on_stylesheet_saved
# yields the filename
# usage: on_stylesheet_saved {|filename| do_something(filename) }
define_callback :stylesheet_saved
# on_stylesheet_error
# yields the filename & message
# usage: on_stylesheet_error {|filename, message| do_something(filename, message) }
define_callback :stylesheet_error
def self.new_from_file(config_file, defaults = nil)
data = new(config_file)
data.with_defaults(defaults) do

View File

@ -68,10 +68,10 @@ module Compass
end
unless @callbacks_loaded
Sass::Plugin.on_updating_stylesheet do |sass_file, css_file|
Compass.configuration.run_callback(:stylesheet_saved, css_file)
Compass.configuration.run_stylesheet_saved(css_file)
end
Sass::Plugin.on_compilation_error do |e, filename, css|
Compass.configuration.run_callback(:stylesheet_error, filename, e.message)
Compass.configuration.run_stylesheet_error(filename, e.message)
end
@callbacks_loaded = true
end

View File

@ -61,6 +61,19 @@ module Compass
inherited_writer(*attributes)
end
def chained_method(method)
line = __LINE__ + 1
class_eval %Q{
alias_method :_chained_#{method}, method
def #{method}(*args, &block)
_chained_#{method}(*args, &block)
if inherited_data
inherited_data.#{method}(*args, &block)
end
end
}, __FILE__, line
end
end

View File

@ -53,7 +53,7 @@ module Compass
cleanup_old_sprites
end
engine.construct_sprite
Compass.configuration.run_callback(:sprite_generated, engine.canvas)
Compass.configuration.run_sprite_generated(engine.canvas)
save!
end
end
@ -89,7 +89,7 @@ module Compass
def save!
FileUtils.mkdir_p(File.dirname(filename))
saved = engine.save(filename)
Compass.configuration.run_callback(:sprite_saved, filename)
Compass.configuration.run_sprite_saved(filename)
saved
end