From 109687c7d03580bfe122a6db12242037f781b26a Mon Sep 17 00:00:00 2001 From: Nico Hagenburger Date: Mon, 18 Oct 2010 00:50:04 +0200 Subject: [PATCH] added error messages to help lemonade users --- .../sass_extensions/functions/sprites.rb | 22 +++++++++--- lib/compass/sprites.rb | 17 +++++++--- spec/sprites_spec.rb | 34 +++++++++++++++++++ 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/lib/compass/sass_extensions/functions/sprites.rb b/lib/compass/sass_extensions/functions/sprites.rb index c6100d01..3ca2c23b 100644 --- a/lib/compass/sass_extensions/functions/sprites.rb +++ b/lib/compass/sass_extensions/functions/sprites.rb @@ -8,7 +8,7 @@ module Compass::SassExtensions::Functions::Sprites last_spacing = 0 width = 0 height = 0 - images = Compass::Sprites.sprites(name) + images = Compass::Sprites.sprites(path, name) # Calculation images.each do |image| @@ -50,7 +50,8 @@ module Compass::SassExtensions::Functions::Sprites sprite_url(uri) end - def sprite_image(uri, x_shift = SASS_NULL, y_shift = SASS_NULL) + def sprite_image(uri, x_shift = SASS_NULL, y_shift = SASS_NULL, depricated_1 = nil, depricated_2 = nil) + check_spacing_deprecation uri, depricated_1, depricated_2 url = sprite_url(uri) position = sprite_position(uri, x_shift, y_shift) Sass::Script::String.new("#{url} #{position}") @@ -61,10 +62,11 @@ module Compass::SassExtensions::Functions::Sprites image_url(Sass::Script::String.new("#{path}.png")) end - def sprite_position(uri, x_shift = SASS_NULL, y_shift = SASS_NULL) - name = File.dirname(uri.value) + def sprite_position(uri, x_shift = SASS_NULL, y_shift = SASS_NULL, depricated_1 = nil, depricated_2 = nil) + check_spacing_deprecation uri, depricated_1, depricated_2 + path, name = Compass::Sprites.path_and_name(uri.value) image_name = File.basename(uri.value, '.png') - image = Compass::Sprites.sprites(name).detect{ |image| image[:name] == image_name } + image = Compass::Sprites.sprites(path, name).detect{ |image| image[:name] == image_name } if x_shift.unit_str == "%" x = x_shift.to_s else @@ -85,4 +87,14 @@ private 0 end end + + def check_spacing_deprecation(uri, spacing_before, spacing_after) + if spacing_before or spacing_after + path, name, image_name = Compass::Sprites.path_and_name(uri.value) + message = %Q(Spacing parameter is deprecated. ) + + %Q(Please add `$#{name}-#{image_name}-spacing: #{spacing_before};` ) + + %Q(before the `@import "#{path}/*.png";` statement.) + raise Compass::Error, message + end + end end diff --git a/lib/compass/sprites.rb b/lib/compass/sprites.rb index 7e4dc532..1796f4a5 100644 --- a/lib/compass/sprites.rb +++ b/lib/compass/sprites.rb @@ -1,6 +1,7 @@ module Compass class Sprites < Sass::Importers::Base attr_accessor :name + attr_accessor :path class << self def reset @@ -13,19 +14,27 @@ module Compass end end - def sprites(name) + def sprites(path, name, create = false) @@sprites = {} if @@sprites.nil? - @@sprites[name] ||= [] + index = "#{path}/#{name}" + images = @@sprites[index] + if images + images + elsif create + images = @@sprites[index] = [] + else + raise Compass::Error, %Q(`@import` statement missing. Please add `@import "#{path}/*.png";`.) + end end end def images - Compass::Sprites.sprites(self.name) + Compass::Sprites.sprites(self.path, self.name, true) end def find(uri, options) if uri =~ /\.png$/ - path, self.name = Compass::Sprites.path_and_name(uri) + self.path, self.name = Compass::Sprites.path_and_name(uri) glob = File.join(Compass.configuration.images_path, uri) Dir.glob(glob).sort.each do |file| width, height = Compass::SassExtensions::Functions::ImageSize::ImageProperties.new(file).size diff --git a/spec/sprites_spec.rb b/spec/sprites_spec.rb index 92aa9c3d..497495b8 100644 --- a/spec/sprites_spec.rb +++ b/spec/sprites_spec.rb @@ -396,4 +396,38 @@ describe Compass::Sprites do CSS end + it "should raise deprication errors for lemonade's spacing syntax" do + proc do + render <<-SCSS + @import "squares/*.png"; + + .squares { + background: sprite-image("squares/20x20.png", 0, 0, 11px) no-repeat; + } + SCSS + end.should raise_error Compass::Error, + %q(Spacing parameter is deprecated. Please add `$squares-20x20-spacing: 11px;` before the `@import "squares/*.png";` statement.) + proc do + render <<-SCSS + @import "squares/*.png"; + + .squares { + background: sprite-position("squares/20x20.png", 0, 0, 11px) no-repeat; + } + SCSS + end.should raise_error Compass::Error, + %q(Spacing parameter is deprecated. Please add `$squares-20x20-spacing: 11px;` before the `@import "squares/*.png";` statement.) + end + + it "should raise an error if @import is missing" do + proc do + render <<-SCSS + .squares { + background: sprite-image("squares/20x20.png") no-repeat; + } + SCSS + end.should raise_error Compass::Error, + %q(`@import` statement missing. Please add `@import "squares/*.png";`.) + end + end \ No newline at end of file