From 29d39e808d3891552c98a96e2b94e38edd44d8ba Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sun, 12 Sep 2010 16:11:29 -0700 Subject: [PATCH] Use the Sass cache to store information about sprites across compiles -- this removes one aspect of lemonade's filesystem dependency. --- .../sass_extensions/monkey_patches/sprites.rb | 2 +- lib/compass/sprites.rb | 23 +++++------ spec/lemonade_spec.rb | 41 +++++++++++-------- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/lib/compass/sass_extensions/monkey_patches/sprites.rb b/lib/compass/sass_extensions/monkey_patches/sprites.rb index 16153756..b736ba77 100644 --- a/lib/compass/sass_extensions/monkey_patches/sprites.rb +++ b/lib/compass/sass_extensions/monkey_patches/sprites.rb @@ -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 diff --git a/lib/compass/sprites.rb b/lib/compass/sprites.rb index b95b5b73..5868feee 100644 --- a/lib/compass/sprites.rb +++ b/lib/compass/sprites.rb @@ -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 diff --git a/spec/lemonade_spec.rb b/spec/lemonade_spec.rb index fbcf141b..ac2f454f 100644 --- a/spec/lemonade_spec.rb +++ b/spec/lemonade_spec.rb @@ -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