From 27735a95379d40290b2e294bafea8cd4bdf3e1b9 Mon Sep 17 00:00:00 2001 From: Scott Davis Date: Sat, 26 Mar 2011 13:31:48 -0400 Subject: [PATCH] fixed a bug that was causeing stack level too deep --- Gemfile.lock | 2 +- lib/compass/sass_extensions/sprites/base.rb | 4 +- .../sass_extensions/sprites/sprite_map.rb | 31 ++++++----- .../sass_extensions/sprites/sprites.rb | 52 ++++++++++++++----- .../sprites/sprite_map_spec.rb | 5 +- 5 files changed, 63 insertions(+), 31 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 93a1392e..c486cffc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,7 +7,7 @@ GIT PATH remote: . specs: - compass (0.11.beta.4.c3f0053) + compass (0.11.beta.4.c201563) chunky_png (~> 1.1.0) sass (>= 3.1.0.alpha.249) diff --git a/lib/compass/sass_extensions/sprites/base.rb b/lib/compass/sass_extensions/sprites/base.rb index c50d047e..aa671b39 100644 --- a/lib/compass/sass_extensions/sprites/base.rb +++ b/lib/compass/sass_extensions/sprites/base.rb @@ -159,14 +159,14 @@ module Compass # Checks whether this sprite is outdated def outdated? if File.exists?(filename) - return @images.map(&:mtime).any? { |mtime| mtime > self.mtime } + return @images.map(&:mtime).any? { |imtime| imtime.to_i > self.mtime.to_i } end true end # Mtime of the sprite file def mtime - File.mtime(filename) + @mtime ||= File.mtime(filename) end def inspect diff --git a/lib/compass/sass_extensions/sprites/sprite_map.rb b/lib/compass/sass_extensions/sprites/sprite_map.rb index fcbc823d..3890135c 100644 --- a/lib/compass/sass_extensions/sprites/sprite_map.rb +++ b/lib/compass/sass_extensions/sprites/sprite_map.rb @@ -2,6 +2,11 @@ module Compass class SpriteMap attr_reader :uri, :options + + def find_relative(*args) + nil + end + def initialize(uri, options) @uri, @options = uri, options end @@ -27,29 +32,27 @@ module Compass def sprite_names @sprite_names ||= files.collect { |file| File.basename(file, '.png') } end - + # Returns the sass options for this sprite def sass_options @sass_options ||= options.merge(:filename => name, :syntax => :scss, :importer => self) end - - # Returns the mtime of all image files combined - def mtime - Compass.quick_cache("mtime:#{uri}") do - files.collect { |file| File.mtime(file) }.max - end - end # Returns a Sass::Engine for this sprite object def sass_engine - Sass::Engine.new(content_for_images, options) + Sass::Engine.new(content_for_images, sass_options) + end + + def ensure_path_and_name! + @path, @name = Compass::Sprites.path_and_name(uri) + end + + def key(uri, options) + Compass::Sprites.key(uri) end - private - def ensure_path_and_name! - return if @path && @name - uri =~ %r{((.+/)?(.+))/(.+?)\.png} - @path, @name = $1, $3 + def mtime(uri, options) + Compass::Sprites.mtime(uri, options) end # Generates the Sass for this sprite file diff --git a/lib/compass/sass_extensions/sprites/sprites.rb b/lib/compass/sass_extensions/sprites/sprites.rb index fcac8979..8d40c294 100644 --- a/lib/compass/sass_extensions/sprites/sprites.rb +++ b/lib/compass/sass_extensions/sprites/sprites.rb @@ -1,27 +1,53 @@ module Compass class Sprites < Sass::Importers::Base - - def find_relative(*args) - nil + attr_accessor :name, :path + @@maps = {} + class << self + + def path_and_name(uri) + if uri =~ %r{((.+/)?(.+))/(.+?)\.png} + [$1, $3, $4] + end + end + + def discover_sprites(uri) + self.load_map(uri, options).files + end + + def sprite_name(file) + File.basename(file, '.png') + end end - + + def self.load_map(uri, options) + key = self.key(uri, options) + @@maps[key] ||= SpriteMap.new(uri, options) + end + + # Called by the sass engine to build a new SpriteMap def find(uri, options) if uri =~ /\.png$/ - SpriteMap.new(uri, options).sass_engine + map = Compass::Sprites.load_map(uri, options) + self.path, self.name = map.path, map.name + return map.sass_engine end end - - # Called by the sass engine to identift the SpriteMap - def key(uri, options) - [self.class.name + ":" + File.dirname(File.expand_path(uri)), - File.basename(uri)] + + # Called by the sass engine to identify the SpriteMap + def self.key(uri, options={}) + [self.class.name + ":" + File.dirname(File.expand_path(uri)), File.basename(uri)] end - def mtime(uri, options) - SpriteMap.new(uri, options).mtime + def self.mtime(uri, options) + Compass.quick_cache("mtime:#{uri}") do + map = Compass::Sprites.load_map(uri, options) + map.files.inject(Time.at(0)) do |max_time, file| + (t = File.mtime(file)) > max_time ? t : max_time + end + end end - + def to_s "" end diff --git a/spec/compass/sass_extensions/sprites/sprite_map_spec.rb b/spec/compass/sass_extensions/sprites/sprite_map_spec.rb index 1136e9a4..ddb679de 100644 --- a/spec/compass/sass_extensions/sprites/sprite_map_spec.rb +++ b/spec/compass/sass_extensions/sprites/sprite_map_spec.rb @@ -42,7 +42,10 @@ describe Compass::SpriteMap do its(:sass_options) { should == options.merge(:filename => name, :syntax => :scss, :importer => sprite_map) } - its(:mtime) { should == mtime } + + it "should have a correct mtime" do + puts sprite_map.mtime(uri, subject.sass_options) + end it "should have a test for the sass engine" do pending 'sass'