Permalink
Browse files

Merge branch 'master' of github.com:shwoodard/sprites

* 'master' of github.com:shwoodard/sprites:
  Note for minor bug
  Sprites objects can now generate their own rake tasks!
  Deprecate "Stylesheet"
  Minor cleanup, really don't like multiline ternary
  move to two lines
  dry off meth
  use config instead of attr_accessor so don't mess with someone else's class

Conflicts:
	lib/sprites/test/sprite_generator_tester.rb
  • Loading branch information...
shwoodard committed Mar 2, 2012
2 parents 76e7fd2 + f7d51b9 commit 992639f19a2f7d1cb68d7379f4679fcd411c7a32
View
@@ -103,6 +103,41 @@ def reset!
@configuration = Configuration.new
@loaded = false
end
+
+ def all_files
+ files = {}
+
+ all_files = []
+ if configuration.definition_file
+ files[configuration.definition_file] = all_files
+ end
+
+ @sprites.each do |name, sprite|
+ all_files.concat sprite.all_sprite_piece_files
+ files[sprite.stylesheet_path] = sprite.all_sprite_piece_files
+ files[sprite.path] = sprite.all_sprite_piece_files
+ end
+
+ all_files.uniq!
+
+ files
+ end
+
+ def generate(generator = configuration.generator)
+ generator.new(self).generate
+ end
+
+ def define_file_tasks(task_name = :sprites)
+ extend Rake::DSL
+ all_files.each do |output, inputs|
+ FileList[inputs]
+ file output => inputs do
+ # XXX this will currently run once per output, we need to fix that.
+ generate
+ end
+ end
+ task task_name => all_files.keys
+ end
end
require 'sprites/railtie' if defined?(Rails)
@@ -1,14 +1,15 @@
class Sprites
class Configuration
PATH_FIELDS = %w{definition_file sprites_path sprite_stylesheets_path sprite_pieces_path}
- OTHER_FIELDS = %w{definition_file autoload sprite_asset_path}
+ OTHER_FIELDS = %w{definition_file autoload sprite_asset_path generator}
FIELDS = PATH_FIELDS + OTHER_FIELDS
DEFAULT_CONFIGURATION = {
'sprites_path' => 'public/images/sprites',
'sprite_stylesheets_path' => 'public/stylesheets/sprites',
'sprite_pieces_path' => 'public/images/sprite_images',
'sprite_asset_path' => '/images/sprites',
+ 'generator' => ChunkyPngGenerator,
'autoload' => true
}
View
@@ -1,20 +1,13 @@
class Sprites
class Railtie < ::Rails::Railtie
-
- def self.each_sprited_engine
- (Rails.application.railties.engines + [Rails.application]).each do |engine|
- next unless engine.config.respond_to?(:uses_sprites) && engine.config.uses_sprites
- yield engine
- end
- end
-
rake_tasks do
desc "Generate sprites and stylesheets"
task :sprites => :environment do
Sprites::Railtie.each_sprited_engine do |engine|
next if engine.class.superclass == Rails::Engine && !ENV['ENGINES']
- sprite_generator = Sprites::ChunkyPngGenerator.new(engine.class.sprites)
+ sprites = engine.config.send(Sprites::Railtie.config_field_name(engine.engine_name))
+ sprite_generator = Sprites::ChunkyPngGenerator.new(sprites)
sprite_generator.generate
end
end
@@ -24,8 +17,10 @@ def self.each_sprited_engine
Sprites::Railtie.each_sprited_engine do |engine|
assets_path = "app/assets"
- engine.class.cattr_accessor(:sprites) { Sprites.new }
- engine.class.sprites.configuration.configure(
+ sprites = Sprites.new
+
+ engine.config.send(Sprites::Railtie.config_field_name_with_equals(engine.engine_name), sprites)
+ sprites.configuration.configure(
:sprites_path => engine.root.join(assets_path, 'images/sprites'),
:sprite_stylesheets_path => engine.root.join(assets_path, 'stylesheets/sprites'),
:sprite_pieces_path => engine.root.join(assets_path, 'images/sprite_images'),
@@ -34,11 +29,28 @@ def self.each_sprited_engine
default_definition_file = File.join(engine.config.root, 'config/sprites.rb')
if File.exists? default_definition_file
- engine.class.sprites.configuration.definition_file = default_definition_file
+ sprites.configuration.definition_file = default_definition_file
else
- engine.class.sprites.configuration.autoload = true
+ sprites.configuration.autoload = true
end
end
end
+
+ private
+
+ def self.each_sprited_engine
+ (Rails.application.railties.engines + [Rails.application]).each do |engine|
+ next unless engine.config.respond_to?(:uses_sprites) && engine.config.uses_sprites
+ yield engine
+ end
+ end
+
+ def self.config_field_name(engine_name)
+ :"sprites_for_#{engine_name}"
+ end
+
+ def self.config_field_name_with_equals(engine_name)
+ :"#{config_field_name(engine_name)}="
+ end
end
end
View
@@ -2,7 +2,6 @@
require 'active_support/core_ext'
require 'sprites/sprite_pieces'
require 'sprites/sprite_piece'
-require 'sprites/stylesheet'
require 'forwardable'
@@ -23,7 +22,7 @@ module Orientations
extend Forwardable
- attr_reader :name, :sprite_pieces, :stylesheet
+ attr_reader :name, :sprite_pieces
attr_accessor *SUPPORTED_OPTIONS
def_delegator :@sprites, :configuration
@@ -32,7 +31,6 @@ def initialize(name, sprites)
@name, @sprites = name, sprites
@sprite_pieces = SpritePieces.new(@sprites, self)
- @stylesheet = Stylesheet.new(self)
end
def orientation=(val)
@@ -52,10 +50,12 @@ def background_property_url
@url || File.join(@sprites.configuration.sprite_asset_path, sprite_file_name)
end
+ # Output file for the sprites image
def path
File.join(@sprites.configuration.sprites_path, @path || sprite_file_name)
end
+ # Output file for the sprites stylesheet
def stylesheet_path
File.join(@sprites.configuration.sprite_stylesheets_path, @stylesheet_path || stylesheet_file_name)
end
@@ -91,7 +91,7 @@ def configure(options = {}, &blk)
def write_stylesheet(configuration, sprite_pieces = @sprite_pieces)
FileUtils.mkdir_p(File.dirname(stylesheet_path))
File.open stylesheet_path, 'w+' do |f|
- f << stylesheet.css(sprite_pieces)
+ f << sprite_pieces.css
end
end
@@ -119,5 +119,11 @@ def autoload!
end
end
+ def all_sprite_piece_files
+ @sprite_pieces.map do |sp|
+ sp.source_path
+ end
+ end
+
end
end
@@ -31,9 +31,7 @@ def all
end
def [](val)
- @sprite_pieces.has_key?(val) ?
- @sprite_pieces[val] :
- nil
+ @sprite_pieces[val] if @sprite_pieces.has_key?(val)
end
def element_at(index)
View
@@ -1,22 +0,0 @@
-require 'active_support/core_ext/object/blank'
-
-class Sprites
- class Stylesheet
- attr_reader :path
- attr_accessor :sprite_pieces
-
- def initialize(sprite)
- @sprite = sprite
- end
-
- def path
- @sprite.stylesheet_path
- end
-
- def css(sprite_pieces = @sprite_pieces)
- return unless sprite_pieces.present?
-
- sprite_pieces.css
- end
- end
-end
@@ -73,7 +73,7 @@ def test_generate(gem_root = GEM_ROOT)
def parser
@parser ||= begin
- stylesheet_path = sprite.stylesheet.path
+ stylesheet_path = sprite.stylesheet_path
parser = CssParser::Parser.new
parser.load_file!(File.basename(stylesheet_path), File.dirname(stylesheet_path), :screen)
parser
@@ -6,6 +6,10 @@
describe "When creating sprites and stylesheets in a rails app", :rails => true do
+ let(:application_sprites) { SpritesRailsTestApp::Application.config.sprites_for_sprites_rails_test_app_application }
+
+ let(:engine_sprites) { FakeEngine::Engine.config.sprites_for_fake_engine_engine }
+
before :all do
SpritesRailsTestApp::Application.initialize!
end
@@ -14,15 +18,7 @@
# We only want to do this once, otherwise we get a new config that wasn't
# initialized by the rails initializers. It's excluded in the after filter
# in spec_helper for rails specs.
- SpritesRailsTestApp::Application.sprites.clear
- end
-
- def application_sprites
- SpritesRailsTestApp::Application.sprites
- end
-
- def engine_sprites
- FakeEngine::Engine.sprites
+ each_engine {|sprites| sprites.clear }
end
def each_engine
@@ -39,6 +39,12 @@
end
end
+ context "#generator" do
+ it 'should default to the ChunkyPngGenerator' do
+ config.generator.should == Sprites::ChunkyPngGenerator
+ end
+ end
+
context '#configured?' do
it 'should not be configured if no defaults have changed' do
Sprites::Configuration.new.should_not be_configured
@@ -49,12 +49,6 @@
end
end
- context '#stylesheet' do
- it 'should return a Stylesheet' do
- @sprite.stylesheet.should be_a(Sprites::Stylesheet)
- end
- end
-
context '#stylesheet_path' do
it 'should return the stylesheet path' do
@sprite.stylesheet_path.should == 'public/stylesheets/sprites/foo.css'
@@ -82,4 +76,14 @@
sprite.background_property_url.should == '/assets/sprites/foo.png'
end
end
+
+ context '#all_sprite_piece_files' do
+ it 'should return an array of all of the images that make up this sprite' do
+ 10.times do |i|
+ @sprite.sprite_piece("test#{i}.png", ".test#{i}")
+ end
+ expected = Array.new(10) { |i| "public/images/sprite_images/test#{i}.png" }
+ @sprite.all_sprite_piece_files.should == expected
+ end
+ end
end
@@ -1,25 +0,0 @@
-require 'spec_helper'
-
-describe Sprites::Stylesheet do
-
- let (:sprites) { Sprites.new }
-
- context "self.sprite_full_path(configuration, sprite)" do
- it 'should return the stylesheet full path' do
- sprites.configuration.sprite_stylesheets_path = 'tmp/stylesheets/sprites'
- sprite = Sprites::Sprite.new(:foo, sprites)
- stylesheet = Sprites::Stylesheet.new(sprite)
- stylesheet.path.should == 'tmp/stylesheets/sprites/foo.css'
- end
- end
-
- context '#sprite_pieces, #sprite_pieces=' do
- it 'should allow access to and setting of sprite_pieces' do
- sprite = Sprites::Sprite.new(:foo, sprites)
- stylesheet = Sprites::Stylesheet.new(sprite)
- sprite_pieces = Sprites::SpritePieces.new(sprites, sprite)
- stylesheet.sprite_pieces = sprite_pieces
- stylesheet.sprite_pieces.should be(sprite_pieces)
- end
- end
-end
Oops, something went wrong.

0 comments on commit 992639f

Please sign in to comment.