Class/Module Index [+]

Quicksearch

Compass::SassExtensions::Sprites::SpriteMethods

Constants

SPRITE_VERSION

Changing this string will invalidate all previously generated sprite images. We should do so only when the packing algorithm changes

Public Instance Methods

cleanup_old_sprites() click to toggle source
# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 74
def cleanup_old_sprites
  Dir[File.join(Compass.configuration.images_path, "#{path}-*.png")].each do |file|
    FileUtils.rm file
  end
end
compute_image_metadata!() click to toggle source

Calculates the overal image dimensions collects image sizes and input parameters for each sprite Calculates the height

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 13
def compute_image_metadata!
  @width = 0
  init_images
  compute_image_positions!
  @height = @images.last.top + @images.last.height
  init_engine
end
compute_image_positions!() click to toggle source

Calculates the overal image dimensions collects image sizes and input parameters for each sprite

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 39
def compute_image_positions!
  @images.each_with_index do |image, index|
    image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100) : image.position.value
    next if index == 0
    last_image = @images[index-1]
    image.top = last_image.top + last_image.height + [image.spacing,  last_image.spacing].max
  end
end
filename() click to toggle source

The on-the-disk filename of the sprite

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 58
def filename
  File.join(Compass.configuration.images_path, "#{path}-s#{uniqueness_hash}.png")
end
generate() click to toggle source

Generate a sprite image if necessary

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 63
def generate
  if generation_required?
    if kwargs.get_var('cleanup').value
      cleanup_old_sprites
    end
    engine.construct_sprite
    Compass.configuration.run_callback(:sprite_generated, engine.canvas)
    save!
  end
end
generation_required?() click to toggle source

Does this sprite need to be generated

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 81
def generation_required?
  !File.exists?(filename) || outdated?
end
image_filenames() click to toggle source

All the full-path filenames involved in this sprite

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 109
def image_filenames
  @images.map(&:file)
end
init_engine() click to toggle source
# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 21
def init_engine
  @engine = eval("::Compass::SassExtensions::Sprites::#{modulize}Engine.new(nil, nil, nil)")
  @engine.width = @width
  @engine.height = @height
  @engine.images = @images
end
init_images() click to toggle source

Creates the Sprite::Image objects for each image and calculates the width

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 29
def init_images
  @images = image_names.collect do |relative_file|
    image = Compass::SassExtensions::Sprites::Image.new(self, relative_file, kwargs)
    @width = [ @width, image.width + image.offset ].max
    image
  end
end
mtime() click to toggle source

Mtime of the sprite file

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 122
def mtime
  @mtime ||= File.mtime(filename)
end
outdated?() click to toggle source

Checks whether this sprite is outdated

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 114
def outdated?
  if File.exists?(filename)
    return @images.map(&:mtime).any? { |imtime| imtime.to_i > self.mtime.to_i }
  end
  true
end
save!() click to toggle source

Saves the sprite engine

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 102
def save!
  saved = engine.save(filename)
  Compass.configuration.run_callback(:sprite_saved, filename)
  saved
end
size() click to toggle source

Calculate the size of the sprite

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 127
def size
  [width, height]
end
uniqueness_hash() click to toggle source

Returns the uniqueness hash for this sprite object

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 86
def uniqueness_hash
  @uniqueness_hash ||= begin
    sum = Digest::MD5.new
    sum << SPRITE_VERSION
    sum << path
    images.each do |image|
      [:relative_file, :height, :width, :repeat, :spacing, :position, :digest].each do |attr|
        sum << image.send(attr).to_s
      end
    end
    sum.hexdigest[0...10]
  end
  @uniqueness_hash
end
validate!() click to toggle source

Validates that the sprite_names are valid sass

# File lib/compass/sass_extensions/sprites/sprite_methods.rb, line 49
def validate!
  for sprite_name in sprite_names
    unless sprite_name =~ /\A#{Sass::SCSS::RX::IDENT}\Z/
      raise Sass::SyntaxError, "#{sprite_name} must be a legal css identifier"
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.