class SpriteFactory::Runner

Constants

PSEUDO_CLASS_ORDER
SUPPORTS_PNGCRUSH

Attributes

config[R]
input[R]

Public Instance Methods

run!(&block) click to toggle source
# File lib/sprite_factory/runner.rb, line 35
def run!(&block)
  raise RuntimeError, "unknown layout #{layout_name}"     if !Layout.respond_to?(layout_name)
  raise RuntimeError, "unknown style #{style_name}"       if !Style.respond_to?(style_name)
  raise RuntimeError, "unknown library #{library_name}"   if !Library.respond_to?(library_name)

  raise RuntimeError, "input must be a single directory"  if input.nil?  || input.to_s.empty? || !File.directory?(input)
  raise RuntimeError, "no image files found"              if image_files.empty?
  raise RuntimeError, "no output file specified"          if output.to_s.empty?
  raise RuntimeError, "no output image file specified"    if output_image_file.to_s.empty?
  raise RuntimeError, "no output style file specified"    if output_style_file.to_s.empty?

  raise RuntimeError, "set :width for fixed width, or :hpadding for horizontal padding, but not both." if width  && !hpadding.zero?
  raise RuntimeError, "set :height for fixed height, or :vpadding for vertical padding, but not both." if height && !vpadding.zero?
  raise RuntimeError, "set :width for fixed width, or :hmargin for horizontal margin, but not both." if width  && !hmargin.zero?
  raise RuntimeError, "set :height for fixed height, or :vmargin for vertical margin, but not both." if height && !hmargin.zero?

  raise RuntimeError, "The legacy :csspath attribute is no longer supported, please use :cssurl instead (see README)" unless @config[:csspath].nil?

  images = load_images
  max    = layout_images(images)
  header = summary(images, max)

  report(header)

  css = []
  css << style_comment(header) unless nocomments?                       # header comment
  css << style(selector, css_url, images, &block)                       # generated styles
  css << IO.read(custom_style_file) if File.exists?(custom_style_file)  # custom styles
  css = css.join("\n")

  create_sprite(images, max[:width], max[:height])

  unless nocss?
    css_file = File.open(output_style_file, "w+")
    css_file.puts css
    css_file.close
  end

  if config[:return] == :images
    images # if caller explicitly asked for detailed images hash instead of generated CSS
  else
    css    # otherwise, default is to return the generated CSS to caller in string form
  end

end

Public Class Methods

new(input, config = {}) click to toggle source
# File lib/sprite_factory/runner.rb, line 16
def initialize(input, config = {})
  @input  = input.to_s[-1] == "/" ? input[0...-1] : input # gracefully ignore trailing slash on input directory name
  @config = config
  @config[:style]      ||= SpriteFactory.style    || :css
  @config[:layout]     ||= SpriteFactory.layout   || :horizontal
  @config[:library]    ||= SpriteFactory.library  || :rmagick
  @config[:selector]   ||= SpriteFactory.selector || 'img.'
  @config[:cssurl]     ||= SpriteFactory.cssurl
  @config[:report]     ||= SpriteFactory.report
  @config[:pngcrush]   ||= SpriteFactory.pngcrush
  @config[:nocomments] ||= SpriteFactory.nocomments
  @config[:separator]  ||= SpriteFactory.separator || '_'
  @config[:glob]       ||= SpriteFactory.glob      || '*'
  @config[:sanitizer]  ||= SpriteFactory.sanitizer
  @config[:exclude]    ||= SpriteFactory.exclude   || []
end