added error messages to help lemonade users

This commit is contained in:
Nico Hagenburger 2010-10-18 00:50:04 +02:00 committed by Chris Eppstein
parent 47cec8151c
commit 109687c7d0
3 changed files with 64 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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