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 53033e473b
commit 57a63c455a
3 changed files with 37 additions and 29 deletions

View File

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

View File

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

View File

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