Sha256: 4365c09955cf10ef0e6bdfe72e177d49b1b62721de5fc45ba84f3b2ed4a9291e

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

module ActiveAssets
  module ActiveSprites
    class SpritePiece
      class ValidationError < StandardError
        attr_reader :sprite_piece, :missing_fields
        def initialize(sprite_piece, missing_fields)
          @sprite, @missing_fields = sprite_piece, missing_fields
          super("#{"Your sprite piece could not be created.  " if @sprite_piece.nil?}  Your sprite piece is invalid. It is missing the following fields: #{@missing_fields.join(', ')}")
        end
      end

      class Mapping
        attr_reader :path, :css_selector

        def initialize(path, css_selector)
          raise ValidationError.new(nil, [:path]) if path.blank?
          @path, @css_selector = path, css_selector
        end
      end

      Details = Struct.new(:sprite_path, :x, :y, :width, :height)

      GEOMETRY_PROPS = [:x, :y, :width, :height]
      attr_reader(*GEOMETRY_PROPS)
      attr_accessor :details
      delegate :path, :css_selector, :to => :mapping

      def configure(mapping, options = {}, &blk)
        @mapping = mapping
        options.each {|k,v| send(k, v)}
        instance_eval(&blk) if block_given?
        self
      end

      def to_s
        return '' if details.nil?
        <<-CSS
#{css_selector}
{
  width:#{width || "#{details.width}px"};
  height:#{height || "#{details.height}px"};
  background:url('#{details.sprite_path}') no-repeat #{x || "#{-details.x}px"} #{y || "#{-details.y}px"};
  display:block;
}
        CSS
      end

      GEOMETRY_PROPS.each do |prop|
        eval <<-METH
          def #{prop}(*args)
            #{prop}, *_ = args
            @#{prop} = #{prop} if #{prop}
            @#{prop}
          end
        METH
      end

      private
        def mapping
          @mapping
        end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_assets-0.2.3 lib/active_assets/active_sprites/sprite_piece.rb