Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactoring and code cleanup
  • Loading branch information
scottdavis committed Dec 10, 2011
1 parent 104f3cb commit f8377d1
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 33 deletions.
22 changes: 13 additions & 9 deletions Guardfile
@@ -1,12 +1,16 @@
guard :test do group :tests do
watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" } guard :test do
watch(%r{^test/.+_test\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
watch(%r{^test/units/.+_test\.rb$}) watch(%r{^test/.+_test\.rb$})
watch('test/test_helper.rb') { "test" } watch(%r{^test/units/.+_test\.rb$})
watch('test/test_helper.rb') { "test" }
end
end end


guard :cucumber do group :features do
watch(%r{^features/.+\.feature$}) guard :cucumber do
watch(%r{^features/support/.+$}) { 'features' } watch(%r{^features/.+\.feature$})
watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' } watch(%r{^features/support/.+$}) { 'features' }
watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
end
end end
2 changes: 2 additions & 0 deletions lib/compass/sass_extensions/sprites.rb
Expand Up @@ -2,11 +2,13 @@
require 'compass/sprite_importer' require 'compass/sprite_importer'


module Compass module Compass
class SpriteException < Exception; end
module SassExtensions module SassExtensions
module Sprites module Sprites
end end
end end
end end

require 'compass/sass_extensions/sprites/image_row' require 'compass/sass_extensions/sprites/image_row'
require 'compass/sass_extensions/sprites/row_fitter' require 'compass/sass_extensions/sprites/row_fitter'
require 'compass/sass_extensions/sprites/image' require 'compass/sass_extensions/sprites/image'
Expand Down
Expand Up @@ -13,7 +13,7 @@ def construct_sprite
@canvas = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT) @canvas = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)
images.each do |image| images.each do |image|
input_png = ChunkyPNG::Image.from_file(image.file) input_png = ChunkyPNG::Image.from_file(image.file)
if image.repeat == "no-repeat" if image.no_repeat?
canvas.replace! input_png, image.left, image.top canvas.replace! input_png, image.left, image.top
else else
x = image.left - (image.left / image.width).ceil * image.width x = image.left - (image.left / image.width).ceil * image.width
Expand Down
24 changes: 22 additions & 2 deletions lib/compass/sass_extensions/sprites/image.rb
Expand Up @@ -6,6 +6,11 @@ class Image
TARGET = %r{[_-]target$} TARGET = %r{[_-]target$}
HOVER = %r{[_-]hover$} HOVER = %r{[_-]hover$}
PARENT = %r{(.+)[-_](.+)$} PARENT = %r{(.+)[-_](.+)$}

REPEAT_X = 'repeat-x'
NO_REPEAT = 'no-repeat'

VALID_REPEATS = [REPEAT_X, NO_REPEAT]


attr_reader :relative_file, :options, :base attr_reader :relative_file, :options, :base
attr_accessor :top, :left attr_accessor :top, :left
Expand Down Expand Up @@ -53,8 +58,23 @@ def get_var_file(var)
end end


# Value of <tt> $#{name}-repeat </tt> or <tt> $repeat </tt> # Value of <tt> $#{name}-repeat </tt> or <tt> $repeat </tt>
def repeat def repeat
@repeat ||= (get_var_file("repeat") || options.get_var("repeat") || Sass::Script::String.new("no-repeat")).value @repeat ||= begin
rep = (get_var_file("repeat") || options.get_var("repeat") || Sass::Script::String.new(NO_REPEAT)).value
unless VALID_REPEATS.include? rep
raise SpriteException, "Invalid option for repeat \"#{rep}\" - valid options are #{VALID_REPEATS.join(', ')}"
end

rep
end
end

def repeat_x?
repeat == REPEAT_X
end

def no_repeat?
repeat == NO_REPEAT
end end


# Value of <tt> $#{name}-position </tt> or <tt> $position </tt> defaults to <tt>0px</tt> # Value of <tt> $#{name}-position </tt> or <tt> $position </tt> defaults to <tt>0px</tt>
Expand Down
7 changes: 7 additions & 0 deletions lib/compass/sass_extensions/sprites/layout_methods.rb
Expand Up @@ -5,6 +5,7 @@ module LayoutMethods
HORIZONTAL = 'horizontal' HORIZONTAL = 'horizontal'
DIAGONAL = 'diagonal' DIAGONAL = 'diagonal'
SMART = 'smart' SMART = 'smart'
VERTICAL = 'vertical'


def smart? def smart?
layout == SMART layout == SMART
Expand All @@ -17,6 +18,10 @@ def horizontal?
def diagonal? def diagonal?
layout == DIAGONAL layout == DIAGONAL
end end

def vertical?
layout == VERTICAL
end


def layout def layout
@layout ||= @kwargs.get_var('layout').value @layout ||= @kwargs.get_var('layout').value
Expand All @@ -43,11 +48,13 @@ def compute_image_positions!
b.size <=> a.size b.size <=> a.size
end end
end end

@width = width_for_vertical_layout @width = width_for_vertical_layout
calulate_vertical_postions calulate_vertical_postions
@height = height_for_vertical_layout @height = height_for_vertical_layout
end end
end end



def calculate_smart_positions def calculate_smart_positions
fitter = ::Compass::SassExtensions::Sprites::RowFitter.new(@images) fitter = ::Compass::SassExtensions::Sprites::RowFitter.new(@images)
Expand Down
9 changes: 8 additions & 1 deletion lib/compass/sass_extensions/sprites/sprite_map.rb
Expand Up @@ -49,7 +49,14 @@ def initialize(sprites, path, name, context, kwargs)
end end


def inspect def inspect
to_s puts 'images'
@images.each do |img|
puts img.file
end
puts "options"
@kwargs.each do |k,v|
puts "#{k}:#{v}"
end
end end


def to_s(kwargs = self.kwargs) def to_s(kwargs = self.kwargs)
Expand Down
3 changes: 1 addition & 2 deletions lib/compass/sass_extensions/sprites/sprite_methods.rb
Expand Up @@ -27,8 +27,7 @@ def init_engine
# Creates the Sprite::Image objects for each image and calculates the width # Creates the Sprite::Image objects for each image and calculates the width
def init_images def init_images
@images = image_names.collect do |relative_file| @images = image_names.collect do |relative_file|
image = Compass::SassExtensions::Sprites::Image.new(self, relative_file, kwargs) Image.new(self, relative_file, kwargs)
image
end end
end end


Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added test/fixtures/sprites/public/images/repeat_x/one.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/sprites/public/images/repeat_x/two.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions test/integrations/sprites_test.rb
Expand Up @@ -374,13 +374,13 @@ def clean(string)


it "should repeat the image" do it "should repeat the image" do
css = render <<-SCSS css = render <<-SCSS
$squares-repeat: repeat; $squares-repeat: repeat-x;
@import "squares/*.png"; @import "squares/*.png";
@include all-squares-sprites; @include all-squares-sprites;
SCSS SCSS
assert_correct css, <<-CSS assert_correct css, <<-CSS
.squares-sprite, .squares-ten-by-ten, .squares-twenty-by-twenty { .squares-sprite, .squares-ten-by-ten, .squares-twenty-by-twenty {
background: url('/squares-sbab486c25a.png') no-repeat; background: url('/squares-s13833277b3.png') no-repeat;
} }
.squares-ten-by-ten { .squares-ten-by-ten {
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -49,7 +49,7 @@ def sprite_map_test(options, uri = URI)
path, name = Compass::SpriteImporter.path_and_name(uri) path, name = Compass::SpriteImporter.path_and_name(uri)
sprite_names = Compass::SpriteImporter.sprite_names(uri) sprite_names = Compass::SpriteImporter.sprite_names(uri)
sass_engine = Compass::SpriteImporter.sass_engine(uri, name, importer, options) sass_engine = Compass::SpriteImporter.sass_engine(uri, name, importer, options)
map = Compass::SassExtensions::Sprites::SpriteMap.new(sprite_names.map{|n| "selectors/#{n}.png"}, path, name, sass_engine, options) map = Compass::SassExtensions::Sprites::SpriteMap.new(sprite_names.map{|n| uri.gsub('*', n)}, path, name, sass_engine, options)
map.options = {:compass => {:logger => Compass::NullLogger.new}} map.options = {:compass => {:logger => Compass::NullLogger.new}}
map map
end end
Expand Down
22 changes: 18 additions & 4 deletions test/units/sprites/image_test.rb
Expand Up @@ -4,10 +4,15 @@


class SpritesImageTest < Test::Unit::TestCase class SpritesImageTest < Test::Unit::TestCase
include SpriteHelper include SpriteHelper

def setup def setup
create_sprite_temp create_sprite_temp
end end


def teardown
clean_up_sprites
end

SPRITE_FILENAME = 'selectors/ten-by-ten.png' SPRITE_FILENAME = 'selectors/ten-by-ten.png'


def sprite_path def sprite_path
Expand Down Expand Up @@ -51,13 +56,22 @@ def test_image(options ={})
assert_nil test_image.parent assert_nil test_image.parent
end end


test 'image type is "global"' do test 'image type is "global" should raise exception' do
image = test_image "selectors_ten_by_ten_repeat" => Sass::Script::String.new('global') assert_raise ::Compass::SpriteException do
assert_equal 'global', image.repeat image = test_image "selectors_ten_by_ten_repeat" => Sass::Script::String.new('global')
image.repeat
end
end end


test 'image type is "no-repeat"' do test 'image type is "no-repeat"' do
assert_equal 'no-repeat', test_image.repeat img = test_image
assert_equal 'no-repeat', img.repeat
assert img.no_repeat?
end

test 'image repeat-x' do
img = test_image "selectors_ten_by_ten_repeat" => Sass::Script::String.new('repeat-x')
assert img.repeat_x?
end end


test 'image position' do test 'image position' do
Expand Down
21 changes: 12 additions & 9 deletions test/units/sprites/layout_test.rb
Expand Up @@ -50,23 +50,23 @@ def horizontal(options= {})
sprite_map_test(opts) sprite_map_test(opts)
end end


# REPEAT-X

# VERTICAL LAYOUT # VERTICAL LAYOUT


it "should have a vertical layout" do it "should have a vertical layout" do
assert_equal [0, 10, 20, 30], vertical.images.map(&:top) vert = vertical
assert_equal [0, 0, 0, 0], vertical.images.map(&:left) assert_equal [0, 10, 20, 30], vert.images.map(&:top)
assert_equal [0, 0, 0, 0], vert.images.map(&:left)
assert vert.vertical?
end end


it "should have a vertical layout with spacing" do it "should have a vertical layout with spacing" do
base = sprite_map_test(@options.merge({"spacing" => Sass::Script::Number.new(10, ['px'])})) vert = sprite_map_test(@options.merge({"spacing" => Sass::Script::Number.new(10, ['px'])}))
assert_equal [0, 20, 40, 60], base.images.map(&:top) assert_equal [0, 20, 40, 60], vert.images.map(&:top)
end end


it "should layout vertical with position" do it "should layout vertical with position" do
base = sprite_map_test("selectors_ten_by_ten_active_position" => Sass::Script::Number.new(10, ['px'])) vert = sprite_map_test("selectors_ten_by_ten_active_position" => Sass::Script::Number.new(10, ['px']))
assert_equal [0, 10, 0, 0], base.images.map(&:left) assert_equal [0, 10, 0, 0], vert.images.map(&:left)
end end


it "should generate vertical sprites in decending order" do it "should generate vertical sprites in decending order" do
Expand All @@ -80,6 +80,7 @@ def horizontal(options= {})
it "should have a smart layout" do it "should have a smart layout" do
base = smart base = smart
base.generate base.generate
assert base.smart?
assert_equal 400, base.width assert_equal 400, base.width
assert_equal 60, base.height assert_equal 60, base.height
assert_equal [[0, 0], [20, 120], [20, 0], [20, 100], [20, 160]], base.images.map {|i| [i.top, i.left]} assert_equal [[0, 0], [20, 120], [20, 0], [20, 100], [20, 160]], base.images.map {|i| [i.top, i.left]}
Expand All @@ -92,6 +93,7 @@ def horizontal(options= {})
it "should generate a diagonal sprite" do it "should generate a diagonal sprite" do
base = diagonal base = diagonal
base.generate base.generate
assert base.diagonal?
assert_equal 40, base.width assert_equal 40, base.width
assert_equal 40, base.height assert_equal 40, base.height
assert_equal [[30, 0], [20, 10], [10, 20], [0, 30]], base.images.map {|i| [i.top, i.left]} assert_equal [[30, 0], [20, 10], [10, 20], [0, 30]], base.images.map {|i| [i.top, i.left]}
Expand All @@ -103,14 +105,15 @@ def horizontal(options= {})


it "should have a horizontal layout" do it "should have a horizontal layout" do
base = horizontal base = horizontal
assert base.horizontal?
assert_equal 10, base.height assert_equal 10, base.height
assert_equal 40, base.width assert_equal 40, base.width
end end


it "should layout images horizontaly" do it "should layout images horizontaly" do
base = horizontal base = horizontal
assert_equal [0, 10, 20, 30], base.images.map(&:left) assert_equal [0, 10, 20, 30], base.images.map(&:left)
assert_equal [0, 0, 0, 0], base.images.map(&:top) assert_equal [0, 0, 0, 0], base.images.map(&:top)
end end


it "should layout horizontaly with spacing" do it "should layout horizontaly with spacing" do
Expand Down
3 changes: 1 addition & 2 deletions test/units/sprites/sprite_map_test.rb
Expand Up @@ -123,10 +123,9 @@ def teardown
config.images_path = @images_tmp_path config.images_path = @images_tmp_path
config.sprite_load_path = [@images_tmp_path, other_folder] config.sprite_load_path = [@images_tmp_path, other_folder]
Compass.add_configuration(config, "sprite_config") Compass.add_configuration(config, "sprite_config")
image = Compass::SassExtensions::Sprites::Image.new(@basegit status, "foo/my.png", {}) image = Compass::SassExtensions::Sprites::Image.new(@base, "foo/my.png", {})
assert_equal File.join(other_folder, 'foo/my.png'), image.file assert_equal File.join(other_folder, 'foo/my.png'), image.file
assert_equal 0, image.size assert_equal 0, image.size
FileUtils.rm_rf other_folder
end end


end end

0 comments on commit f8377d1

Please sign in to comment.