Sha256: afcf2f6130f6390ca72a6abc21c553842320e07b0d546a331f039daace41a147

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

module ActiveAssets
  module ActiveSprites
    class Sprites
      def initialize
        @sprites = Hash.new do |sprites, name|
          sprites[name] = Sprite.new
        end
      end

      def describe(&blk)
        instance_eval(&blk) if block_given?
        self
      end

      def all
        @sprites.values
      end

      def sprite(*args, &blk)
        sprite_path, stylesheet_path, options, as =
        case args.first
        when Hash
          options = args.shift
          args = *options.find {|k,v| k.is_a?(String) }
          (args << options).tap {|a| a.last.delete(a.first)}
        when Symbol
          # todo make default paths configurable
          ["sprites/#{args.first.to_s}.png", "sprites/#{args.first.to_s}.css", args.extract_options!, args.first]
        when String
          path = args.first
          [path, "#{File.dirname(path)}/#{File.basename(path, File.extname(path))}.css", args.extract_options!]
        end
        options.reverse_merge!(:as => as)
        @sprites[options[:as] || sprite_path].configure(sprite_path, stylesheet_path, options, &blk)
      end

      def [](name)
        return nil unless @sprites.has_key?(name)
        @sprites[name]
      end

      def clear
        @sprites.clear
      end

      def generate!
        begin
          require 'rmagick'
          Runner.new(@sprites).generate!
        rescue LoadError
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_assets-0.2.1 lib/active_assets/active_sprites/sprites.rb
active_assets-0.2.0 lib/active_assets/active_sprites/sprites.rb
active_assets-0.2.0.rc4 lib/active_assets/active_sprites/sprites.rb