Class: Sprite::ImageWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/sprite/image_writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ImageWriter

Returns a new instance of ImageWriter.



3
4
5
# File 'lib/sprite/image_writer.rb', line 3

def initialize(config)
  @config = config
end

Instance Method Details

#image_output_path(name, format, relative = false) ⇒ Object

get the disk path for a location within the image output folder



20
21
22
23
24
25
26
27
# File 'lib/sprite/image_writer.rb', line 20

def image_output_path(name, format, relative = false)
  path_parts = []
  path_parts << Config.chop_trailing_slash(@config['image_output_path']) if Config.path_present?(@config['image_output_path'])

  cache_buster = "-#{@config['cache_buster']}" if @config['cache_buster']
  path_parts << "#{name}#{cache_buster}.#{format}"
  Config.new(@config).public_path(File.join(*path_parts), relative)
end

#write(image, name, format, quality = nil, background_color = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/sprite/image_writer.rb', line 7

def write(image, name, format, quality = nil, background_color = nil)
  # set up path
  path = image_output_path(name, format)
  FileUtils.mkdir_p(File.dirname(path))
  
  # write sprite image file to disk
  image.write(path) {
    self.quality = quality unless quality.nil?
    self.background_color = background_color unless background_color.nil?
  }
end