Use the Sass cache to store information about sprites across compiles -- this removes one aspect of lemonade's filesystem dependency.

This commit is contained in:
Chris Eppstein 2010-09-12 16:11:29 -07:00
parent 4b75ef471e
commit 29d39e808d
3 changed files with 37 additions and 29 deletions

View File

@ -7,7 +7,7 @@ module Sass
alias_method :render_without_sprites, :render
def render
if result = render_without_sprites
Compass::Sprites.generate_sprites
Compass::Sprites.generate_sprites(options)
result = ERB.new(result).result(binding)
Compass::Sprites.reset
return result

View File

@ -31,30 +31,29 @@ module Compass::Sprites
@@sprites = {}
end
def generate_sprites
def generate_sprites(options)
sprites.each do |sprite_name, sprite|
calculate_sprite sprite
if sprite_changed?(sprite_name, sprite)
if sprite_changed?(sprite_name, sprite, options)
generate_sprite_image sprite
remember_sprite_info! sprite_name, sprite
remember_sprite_info! sprite_name, sprite, options
end
end
end
def sprite_changed?(sprite_name, sprite)
existing_sprite_info = YAML.load(File.read(sprite_info_file(sprite_name)))
def sprite_changed?(sprite_name, sprite, options)
existing_sprite_info = options[:cache_store].retrieve("_#{sprite_name}_data", "") || {}
existing_sprite_info[:sprite] != sprite or existing_sprite_info[:timestamps] != timestamps(sprite)
rescue
true
end
def remember_sprite_info!(sprite_name, sprite)
File.open(sprite_info_file(sprite_name), 'w') do |file|
file << {
:sprite => sprite,
:timestamps => timestamps(sprite),
}.to_yaml
end
def remember_sprite_info!(sprite_name, sprite, options)
data = {
:sprite => sprite,
:timestamps => timestamps(sprite),
}
options[:cache_store].store("_#{sprite_name}_data", "", data)
end
private

View File

@ -20,45 +20,54 @@ describe Compass::Sprites do
###
describe '#remember_sprite_info' do
subject { Compass::Sprites }
it 'should save sprite info into a file' do
File.should_receive(:open).with(File.join('image_path', 'the_sprite.sprite_info.yml'), 'w').and_yield(@file)
@file.should_receive(:<<)
subject.remember_sprite_info!('the_sprite', @sprite)
before :each do
@options = {
:cache_store => Sass::InMemoryCacheStore.new
}
end
it 'should save sprite info to the sass cache' do
subject.remember_sprite_info!('the_sprite', @sprite, @options)
@options[:cache_store].retrieve('_the_sprite_data', "")[:sprite].should == @sprite
end
end
###
describe '#sprite_changed?' do
subject { Compass::Sprites }
before :each do
@options = {
:cache_store => Sass::InMemoryCacheStore.new
}
end
it 'should be false if nothing changed' do
File.should_receive(:open).and_yield(@file)
subject.remember_sprite_info!('the sprite', @sprite)
subject.sprite_changed?('the sprite', @sprite).should be_false
subject.remember_sprite_info!('the sprite', @sprite, @options)
subject.sprite_changed?('the sprite', @sprite, @options).should be_false
end
it 'should be true if the sprite info has changed' do
File.should_receive(:open).and_yield(@file)
subject.remember_sprite_info!('the sprite', @sprite)
subject.remember_sprite_info!('the sprite', @sprite, @options)
@sprite[:info] = 'changed info'
subject.sprite_changed?('the sprite', @sprite).should be_true
subject.sprite_changed?('the sprite', @sprite, @options).should be_true
end
it 'should be true if the images changed' do
File.should_receive(:open).and_yield(@file)
subject.remember_sprite_info!('the sprite', @sprite)
subject.remember_sprite_info!('the sprite', @sprite, @options)
@sprite[:images] = []
subject.sprite_changed?('the sprite', @sprite).should be_true
subject.sprite_changed?('the sprite', @sprite, @options).should be_true
end
it 'should be true if a images timestamp changed' do
File.should_receive(:open).and_yield(@file)
subject.remember_sprite_info!('the sprite', @sprite)
subject.remember_sprite_info!('the sprite', @sprite, @options)
File.stub!(:ctime => Time.now)
subject.sprite_changed?('the sprite', @sprite).should be_true
subject.sprite_changed?('the sprite', @sprite, @options).should be_true
end
end