From c73d281e58f731f8d9e74430d1ebc21bfb8ca94b Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sun, 5 Jun 2011 18:06:37 -0700 Subject: [PATCH 1/2] Move the sprite importer to the compass module and rename it to be more clear about what it does. --- lib/compass/commands/sprite.rb | 2 +- lib/compass/configuration/adapters.rb | 4 ++-- lib/compass/sass_extensions/sprites.rb | 2 +- lib/compass/sass_extensions/sprites/base.rb | 17 ++++++++--------- .../sprite_map.rb => sprite_importer.rb} | 4 ++-- .../sass_extensions/sprites/base_spec.rb | 2 +- .../sass_extensions/sprites/sprite_map_spec.rb | 2 +- test/units/sprites/base_test.rb | 6 +++--- test/units/sprites/image_test.rb | 4 ++-- 9 files changed, 21 insertions(+), 22 deletions(-) rename lib/compass/{sass_extensions/sprites/sprite_map.rb => sprite_importer.rb} (98%) diff --git a/lib/compass/commands/sprite.rb b/lib/compass/commands/sprite.rb index 2b33f9fc..b1c8bee9 100644 --- a/lib/compass/commands/sprite.rb +++ b/lib/compass/commands/sprite.rb @@ -39,7 +39,7 @@ module Compass def perform relative_uri = options[:uri].gsub(/^#{Compass.configuration.images_dir}\//, '') - sprites = Compass::SpriteMap.new(relative_uri, Compass.sass_engine_options) + sprites = Compass::SpriteImporter.new(relative_uri, Compass.sass_engine_options) options[:output_file] ||= File.join(Compass.configuration.sass_path, "sprites", "_#{sprites.name}.#{Compass.configuration.preferred_syntax}") options[:skip_overrides] ||= false contents = sprites.content_for_images(options[:skip_overrides]) diff --git a/lib/compass/configuration/adapters.rb b/lib/compass/configuration/adapters.rb index 6a35c91a..352eb3c4 100644 --- a/lib/compass/configuration/adapters.rb +++ b/lib/compass/configuration/adapters.rb @@ -23,7 +23,7 @@ module Compass plugin_opts[:cache_location] = cache_path unless cache_path.nil? plugin_opts.merge!(sass_options || {}) plugin_opts[:load_paths] ||= [] - plugin_opts[:load_paths] << Compass::SpriteMap.new + plugin_opts[:load_paths] << Compass::SpriteImporter.new plugin_opts end @@ -63,7 +63,7 @@ module Compass next p if p.respond_to?(:find_relative) Sass::Importers::Filesystem.new(p.to_s) end - load_paths << Compass::SpriteMap.new + load_paths << Compass::SpriteImporter.new load_paths end end diff --git a/lib/compass/sass_extensions/sprites.rb b/lib/compass/sass_extensions/sprites.rb index 12e821ed..72a00efa 100644 --- a/lib/compass/sass_extensions/sprites.rb +++ b/lib/compass/sass_extensions/sprites.rb @@ -1,5 +1,5 @@ require 'digest/md5' -require 'compass/sass_extensions/sprites/sprite_map' +require 'compass/sprite_importer' require 'compass/sass_extensions/sprites/image' require 'compass/sass_extensions/sprites/base' require 'compass/sass_extensions/sprites/engines' diff --git a/lib/compass/sass_extensions/sprites/base.rb b/lib/compass/sass_extensions/sprites/base.rb index 3051248a..bac3d821 100644 --- a/lib/compass/sass_extensions/sprites/base.rb +++ b/lib/compass/sass_extensions/sprites/base.rb @@ -4,14 +4,14 @@ module Compass class Base < Sass::Script::Literal - # Initialize a new aprite object from a relative file path + # Initialize a new sprite object from a relative file path # the path is relative to the images_path confguration option def self.from_uri(uri, context, kwargs) - sprite_map = ::Compass::SpriteMap.new(:uri => uri.value, :options => {}) - sprites = sprite_map.files.map do |sprite| + importer = ::Compass::SpriteImporter.new(:uri => uri.value, :options => {}) + sprites = importer.files.map do |sprite| sprite.gsub(Compass.configuration.images_path+"/", "") end - new(sprites, sprite_map, context, kwargs) + new(sprites, importer.path, importer.name, context, kwargs) end # Loads the sprite engine @@ -23,22 +23,21 @@ module Compass # We should do so only when the packing algorithm changes SPRITE_VERSION = "1" - attr_accessor :image_names, :path, :name, :map, :kwargs + attr_accessor :image_names, :path, :name, :kwargs attr_accessor :images, :width, :height - def initialize(sprites, sprite_map, context, kwargs) + def initialize(sprites, path, name, context, kwargs) require_engine! @image_names = sprites - @path = sprite_map.path - @name = sprite_map.name + @path = path + @name = name @kwargs = kwargs @kwargs['cleanup'] ||= Sass::Script::Bool.new(true) @images = nil @width = nil @height = nil @evaluation_context = context - @map = sprite_map validate! compute_image_metadata! end diff --git a/lib/compass/sass_extensions/sprites/sprite_map.rb b/lib/compass/sprite_importer.rb similarity index 98% rename from lib/compass/sass_extensions/sprites/sprite_map.rb rename to lib/compass/sprite_importer.rb index b89b072f..1b02ccba 100644 --- a/lib/compass/sass_extensions/sprites/sprite_map.rb +++ b/lib/compass/sprite_importer.rb @@ -1,12 +1,12 @@ module Compass - class SpriteMap < Sass::Importers::Base + class SpriteImporter < Sass::Importers::Base attr_accessor :uri, :options VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/ SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)\.png} def self.load(uri, options) Compass.quick_cache "Sprite_map:#{uri}#{options.inspect}", 5 do - klass = Compass::SpriteMap.new + klass = Compass::SpriteImporter.new klass.uri, klass.options = uri, options klass end diff --git a/spec/compass/sass_extensions/sprites/base_spec.rb b/spec/compass/sass_extensions/sprites/base_spec.rb index 6a086737..1d8cfedc 100644 --- a/spec/compass/sass_extensions/sprites/base_spec.rb +++ b/spec/compass/sass_extensions/sprites/base_spec.rb @@ -11,7 +11,7 @@ describe Compass::SassExtensions::Sprites::Base do Compass.configure_sass_plugin! #fix this eww options = Compass.sass_engine_options.extend Compass::SassExtensions::Functions::Sprites::VariableReader - @map = Compass::SpriteMap.new("selectors/*.png", options) + @map = Compass::SpriteImporter.new("selectors/*.png", options) @base = Compass::SassExtensions::Sprites::Base.new(@map.sprite_names.map{|n| "selectors/#{n}.png"}, @map.path, 'selectors', @map.sass_engine, @map.options) end diff --git a/spec/compass/sass_extensions/sprites/sprite_map_spec.rb b/spec/compass/sass_extensions/sprites/sprite_map_spec.rb index a6eee9ea..30a3895d 100644 --- a/spec/compass/sass_extensions/sprites/sprite_map_spec.rb +++ b/spec/compass/sass_extensions/sprites/sprite_map_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' require 'fakefs/spec_helpers' require 'timecop' -describe Compass::SpriteMap do +describe Compass::SpriteImporter do include FakeFS::SpecHelpers let(:sprite_map) { self.class.describes.new(uri, options) } diff --git a/test/units/sprites/base_test.rb b/test/units/sprites/base_test.rb index b557bf29..f62ed0cf 100644 --- a/test/units/sprites/base_test.rb +++ b/test/units/sprites/base_test.rb @@ -16,8 +16,8 @@ class SpritesBaseTest < Test::Unit::TestCase end def setup_map - @map = Compass::SpriteMap.new(:uri => "selectors/*.png", :options => @options) - @base = Compass::SassExtensions::Sprites::Base.new(@map.sprite_names.map{|n| "selectors/#{n}.png"}, @map, @map.sass_engine, @map.options) + @importer = Compass::SpriteImporter.new(:uri => "selectors/*.png", :options => @options) + @base = Compass::SassExtensions::Sprites::Base.new(@importer.sprite_names.map{|n| "selectors/#{n}.png"}, @importer.path, @importer.name, @importer.sass_engine, @importer.options) end def teardown @@ -29,7 +29,7 @@ class SpritesBaseTest < Test::Unit::TestCase end it "should have the sprite names" do - assert_equal @map.sprite_names, @base.sprite_names + assert_equal @importer.sprite_names, @base.sprite_names end it 'should have image filenames' do diff --git a/test/units/sprites/image_test.rb b/test/units/sprites/image_test.rb index f5a11a50..ae060c35 100644 --- a/test/units/sprites/image_test.rb +++ b/test/units/sprites/image_test.rb @@ -19,8 +19,8 @@ class SpritesImageTest < Test::Unit::TestCase let(:sprite_name) { File.basename(sprite_filename, '.png') } def parent - map = Compass::SpriteMap.new(:uri => "selectors/*.png", :options => options) - @parent ||= Compass::SassExtensions::Sprites::Base.new(map.sprite_names.map{|n| "selectors/#{n}.png"}, map, map.sass_engine, map.options) + importer = Compass::SpriteImporter.new(:uri => "selectors/*.png", :options => options) + @parent ||= Compass::SassExtensions::Sprites::Base.new(importer.sprite_names.map{|n| "selectors/#{n}.png"}, importer.path, importer.name, importer.sass_engine, importer.options) end let(:options) do From 008cffa3ef938d0243dae8df097ffeea47d63778 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Mon, 6 Jun 2011 18:52:22 -0700 Subject: [PATCH 2/2] Rename the sprite Base class to SpriteMap --- lib/compass/sass_extensions/functions/sprites.rb | 6 +++--- lib/compass/sass_extensions/sprites.rb | 11 ++++++----- .../sprites/{base.rb => sprite_map.rb} | 2 +- test/units/sprites/image_test.rb | 2 +- .../sprites/{base_test.rb => sprite_map_test.rb} | 4 ++-- 5 files changed, 13 insertions(+), 12 deletions(-) rename lib/compass/sass_extensions/sprites/{base.rb => sprite_map.rb} (99%) rename test/units/sprites/{base_test.rb => sprite_map_test.rb} (92%) diff --git a/lib/compass/sass_extensions/functions/sprites.rb b/lib/compass/sass_extensions/functions/sprites.rb index 69b7599e..0bd54ff5 100644 --- a/lib/compass/sass_extensions/functions/sprites.rb +++ b/lib/compass/sass_extensions/functions/sprites.rb @@ -10,7 +10,7 @@ module Compass::SassExtensions::Functions::Sprites end end - # Creates a Compass::SassExtensions::Sprites::Base object. A sprite map, when used in a property is the same + # Creates a Compass::SassExtensions::Sprites::SpriteMap object. A sprite map, when used in a property is the same # as calling sprite-url. So the following background properties are equivalent: # # $icons: sprite-map("icons/*.png"); @@ -21,7 +21,7 @@ module Compass::SassExtensions::Functions::Sprites # the first time it is converted to a url. Simply constructing it has no side-effects. def sprite_map(glob, kwargs = {}) kwargs.extend VariableReader - Compass::SassExtensions::Sprites::Base.from_uri(glob, self, kwargs) + Compass::SassExtensions::Sprites::SpriteMap.from_uri(glob, self, kwargs) end Sass::Script::Functions.declare :sprite_map, [:glob], :var_kwargs => true @@ -160,7 +160,7 @@ protected end def verify_map(map, error = "sprite") - unless map.is_a?(Compass::SassExtensions::Sprites::Base) + unless map.is_a?(Compass::SassExtensions::Sprites::SpriteMap) missing_sprite!(error) end end diff --git a/lib/compass/sass_extensions/sprites.rb b/lib/compass/sass_extensions/sprites.rb index 72a00efa..d22ab96b 100644 --- a/lib/compass/sass_extensions/sprites.rb +++ b/lib/compass/sass_extensions/sprites.rb @@ -1,13 +1,14 @@ require 'digest/md5' require 'compass/sprite_importer' -require 'compass/sass_extensions/sprites/image' -require 'compass/sass_extensions/sprites/base' -require 'compass/sass_extensions/sprites/engines' module Compass module SassExtensions module Sprites - end end -end \ No newline at end of file +end + +require 'compass/sass_extensions/sprites/image' +require 'compass/sass_extensions/sprites/sprite_map' +require 'compass/sass_extensions/sprites/engines' + diff --git a/lib/compass/sass_extensions/sprites/base.rb b/lib/compass/sass_extensions/sprites/sprite_map.rb similarity index 99% rename from lib/compass/sass_extensions/sprites/base.rb rename to lib/compass/sass_extensions/sprites/sprite_map.rb index bac3d821..2bdf905c 100644 --- a/lib/compass/sass_extensions/sprites/base.rb +++ b/lib/compass/sass_extensions/sprites/sprite_map.rb @@ -1,7 +1,7 @@ module Compass module SassExtensions module Sprites - class Base < Sass::Script::Literal + class SpriteMap < Sass::Script::Literal # Initialize a new sprite object from a relative file path diff --git a/test/units/sprites/image_test.rb b/test/units/sprites/image_test.rb index ae060c35..aa8dff32 100644 --- a/test/units/sprites/image_test.rb +++ b/test/units/sprites/image_test.rb @@ -20,7 +20,7 @@ class SpritesImageTest < Test::Unit::TestCase def parent importer = Compass::SpriteImporter.new(:uri => "selectors/*.png", :options => options) - @parent ||= Compass::SassExtensions::Sprites::Base.new(importer.sprite_names.map{|n| "selectors/#{n}.png"}, importer.path, importer.name, importer.sass_engine, importer.options) + @parent ||= Compass::SassExtensions::Sprites::SpriteMap.new(importer.sprite_names.map{|n| "selectors/#{n}.png"}, importer.path, importer.name, importer.sass_engine, importer.options) end let(:options) do diff --git a/test/units/sprites/base_test.rb b/test/units/sprites/sprite_map_test.rb similarity index 92% rename from test/units/sprites/base_test.rb rename to test/units/sprites/sprite_map_test.rb index f62ed0cf..0587657f 100644 --- a/test/units/sprites/base_test.rb +++ b/test/units/sprites/sprite_map_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class SpritesBaseTest < Test::Unit::TestCase +class SpriteMapTest < Test::Unit::TestCase def setup Hash.send(:include, Compass::SassExtensions::Functions::Sprites::VariableReader) @@ -17,7 +17,7 @@ class SpritesBaseTest < Test::Unit::TestCase def setup_map @importer = Compass::SpriteImporter.new(:uri => "selectors/*.png", :options => @options) - @base = Compass::SassExtensions::Sprites::Base.new(@importer.sprite_names.map{|n| "selectors/#{n}.png"}, @importer.path, @importer.name, @importer.sass_engine, @importer.options) + @base = Compass::SassExtensions::Sprites::SpriteMap.new(@importer.sprite_names.map{|n| "selectors/#{n}.png"}, @importer.path, @importer.name, @importer.sass_engine, @importer.options) end def teardown