Don't rely on global callbacks

This commit is contained in:
Chris Eppstein 2011-02-20 10:16:11 -08:00
parent 2142430735
commit 2fe1c17ac4
13 changed files with 139 additions and 128 deletions

View File

@ -350,7 +350,7 @@ to avoid crashing the watcher in the case where the file has been removed.
on_stylesheet_saved do |filename|
Growl.notify {
self.message "#{filename} updated!"
self.message "#{File.basename(filename)} updated!"
self.icon = '/path/to/success.jpg'
}
end
@ -359,7 +359,7 @@ to avoid crashing the watcher in the case where the file has been removed.
on_stylesheet_error do |filename, message|
Growl.notify {
self.message = "#{filename}: #{message}"
self.message = "#{File.basename(filename)}: #{message}"
self.icon = '/path/to/fail.jpg'
sticky!
}

View File

@ -193,7 +193,7 @@ end
Then /^the following configuration properties are set in ([^ ]+):$/ do |config_file, table|
config = Compass::Configuration::Data.new_from_file(config_file)
config = Compass::Configuration::FileData.new_from_file(config_file)
table.hashes.each do |hash|
config.send(hash['property']).should == hash['value']
end

View File

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

View File

@ -43,6 +43,6 @@ module Compass
end
end
['adapters', 'callbacks', 'comments', 'defaults', 'helpers', 'inheritance', 'serialization', 'paths', 'data'].each do |lib|
['adapters', 'comments', 'defaults', 'helpers', 'inheritance', 'serialization', 'paths', 'data', 'file_data'].each do |lib|
require "compass/configuration/#{lib}"
end

View File

@ -1,25 +0,0 @@
module Compass
module Configuration
module CallbackMethods
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
define_callback :stylesheet_saved
define_callback :stylesheet_error
end
class Callbacks
extend CallbackMethods
end
end
end

View File

@ -135,6 +135,16 @@ 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

@ -0,0 +1,43 @@
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
data._parse(config_file)
end
data
end
def self.new_from_string(contents, filename, defaults = nil)
data = new(filename)
data.with_defaults(defaults) do
data.parse_string(contents, filename)
end
data
end
end
end
end

View File

@ -34,11 +34,11 @@ module Compass
config
elsif config.respond_to?(:read)
filename ||= config.to_s if config.is_a?(Pathname)
Compass::Configuration::Data.new_from_string(config.read, filename, defaults)
Compass::Configuration::FileData.new_from_string(config.read, filename, defaults)
elsif config.is_a?(Hash)
Compass::Configuration::Data.new(filename, config)
elsif config.is_a?(String)
Compass::Configuration::Data.new_from_file(config, defaults)
Compass::Configuration::FileData.new_from_file(config, defaults)
elsif config.is_a?(Symbol)
Compass::AppIntegration.lookup(config).configuration
else

View File

@ -144,8 +144,6 @@ module Compass
def method_missing(meth, *args, &block)
if inherited_data
inherited_data.send(meth, *args, &block)
elsif Callbacks.respond_to?(meth, true)
Callbacks.send(meth, *args, &block)
else
raise NoMethodError, meth.to_s
end

View File

@ -2,30 +2,6 @@ module Compass
module Configuration
# The serialization module manages reading and writing the configuration file(s).
module Serialization
def self.included(base)
base.send(:include, InstanceMethods)
base.extend ClassMethods
end
module ClassMethods
def new_from_file(config_file, defaults = nil)
data = Data.new(config_file)
data.with_defaults(defaults) do
data._parse(config_file)
end
data
end
def new_from_string(contents, filename, defaults = nil)
data = Data.new(filename)
data.with_defaults(defaults) do
data.parse_string(contents, filename)
end
data
end
end
module InstanceMethods
def parse(config_file)
raise Compass::Error, "Compass.configuration.parse(filename) has been removed. Please call Compass.add_project_configuration(filename) instead."
end
@ -97,5 +73,4 @@ module Compass
end
end
end
end

View File

@ -123,8 +123,9 @@ module Compass::SassExtensions::Functions::Sprites
# Generate a sprite image if necessary
def generate
if generation_required?
save!(construct_sprite)
Compass.configuration.send(:run_sprite_generated, construct_sprite)
sprite_data = construct_sprite
save!(sprite_data)
Compass.configuration.run_callback(:sprite_generated, sprite_data)
end
end
@ -182,7 +183,7 @@ module Compass::SassExtensions::Functions::Sprites
# saves the sprite for later retrieval
def save!(output_png)
saved = output_png.save filename
Compass.configuration.send(:run_sprite_saved, filename)
Compass.configuration.run_callback(:sprite_saved, filename)
saved
end

View File

@ -8,7 +8,8 @@ describe Compass::Sprites do
@images_src_path = File.join(File.dirname(__FILE__), 'test_project', 'public', 'images')
@images_tmp_path = File.join(File.dirname(__FILE__), 'test_project', 'public', 'images-tmp')
FileUtils.cp_r @images_src_path, @images_tmp_path
Compass.configuration.images_path = @images_tmp_path
file = StringIO.new("images_path = #{@images_tmp_path.inspect}\n")
Compass.add_configuration(file, "sprite_config")
Compass.configure_sass_plugin!
end

View File

@ -23,8 +23,11 @@ class CompassTest < Test::Unit::TestCase
def test_on_stylesheet_saved_callback
saved = false
filepath = nil
Compass.configuration.on_stylesheet_saved {|filepath| path = filepath; saved = true }
within_project(:blueprint) { } #requires a block but we don't need to pass anything - sdavis
config = nil
before_compile = Proc.new do |config|
config.on_stylesheet_saved {|filepath| path = filepath; saved = true }
end
within_project(:blueprint, before_compile)
assert saved, "Stylesheet callback didn't get called"
assert filepath.is_a?(String), "Path is not a string"
end
@ -122,17 +125,22 @@ private
end
end
def within_project(project_name)
def within_project(project_name, config_block = nil)
@current_project = project_name
Compass.add_configuration(configuration_file(project_name)) if File.exists?(configuration_file(project_name))
Compass.configuration.project_path = project_path(project_name)
Compass.configuration.environment = :production
args = Compass.configuration.to_compiler_arguments(:logger => Compass::NullLogger.new)
if config_block
config_block.call(Compass.configuration)
end
if Compass.configuration.sass_path && File.exists?(Compass.configuration.sass_path)
compiler = Compass::Compiler.new *args
compiler.run
end
yield Compass.configuration
yield Compass.configuration if block_given?
rescue
save_output(project_name)
raise