Skip to content

Commit

Permalink
Use the Sass cache to store information about sprites across compiles…
Browse files Browse the repository at this point in the history
… -- this removes one aspect of lemonade's filesystem dependency.
  • Loading branch information
chriseppstein committed Nov 28, 2010
1 parent 4b75ef4 commit 29d39e8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/compass/sass_extensions/monkey_patches/sprites.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RootNode < Node
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
Expand Down
23 changes: 11 additions & 12 deletions lib/compass/sprites.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,29 @@ def reset
@@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
Expand Down
41 changes: 25 additions & 16 deletions spec/lemonade_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,54 @@
###

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
Expand Down

0 comments on commit 29d39e8

Please sign in to comment.