class ImageProcessing::Pipeline

Constants

DEFAULT_FORMAT

Attributes

destination[R]
format[R]
loader[R]
operations[R]
processor[R]
saver[R]

Public Class Methods

new(options) click to toggle source

Initializes the pipeline with all the processing options.

# File lib/image_processing/pipeline.rb, line 10
def initialize(options)
  fail Error, "source file is not provided" unless options[:source]

  options.each do |name, value|
    instance_variable_set(:"@#{name}", value)
  end
end

Public Instance Methods

call(save: true) click to toggle source

Determines the destination and calls the processor.

# File lib/image_processing/pipeline.rb, line 19
def call(save: true)
  if save == false
    call_processor
  elsif destination
    handle_destination do
      call_processor(destination: destination)
    end
  else
    create_tempfile do |tempfile|
      call_processor(destination: tempfile.path)
    end
  end
end
destination_format() click to toggle source

Determines the appropriate destination image format.

# File lib/image_processing/pipeline.rb, line 39
def destination_format
  format   = determine_format(destination) if destination
  format ||= self.format
  format ||= determine_format(source_path) if source_path

  format || DEFAULT_FORMAT
end
source_path() click to toggle source

Retrieves the source path on disk.

# File lib/image_processing/pipeline.rb, line 34
def source_path
  source if source.is_a?(String)
end

Private Instance Methods

call_processor(**options) click to toggle source
# File lib/image_processing/pipeline.rb, line 49
def call_processor(**options)
  processor.call(
    source:     source,
    loader:     loader,
    operations: operations,
    saver:      saver,
    **options
  )
end
create_tempfile() { |tempfile| ... } click to toggle source

Creates a new tempfile for the destination file, yields it, and refreshes the file descriptor to get the updated file.

# File lib/image_processing/pipeline.rb, line 61
def create_tempfile
  tempfile = Tempfile.new(["image_processing", ".#{destination_format}"], binmode: true)

  yield tempfile

  tempfile.open
  tempfile
rescue
  tempfile.close! if tempfile
  raise
end
determine_format(file_path) click to toggle source
# File lib/image_processing/pipeline.rb, line 97
def determine_format(file_path)
  extension = File.extname(file_path)

  extension[1..-1] if extension.size > 1
end
handle_destination() { || ... } click to toggle source

In case of processing errors, both libvips and imagemagick will leave the empty destination file they created, so this method makes sure it is deleted in case an exception is raised on saving the image.

# File lib/image_processing/pipeline.rb, line 76
def handle_destination
  destination_existed = File.exist?(destination)
  yield
rescue
  File.delete(destination) if File.exist?(destination) && !destination_existed
  raise
end
source() click to toggle source

Converts the source image object into a path or the accumulator object.

# File lib/image_processing/pipeline.rb, line 85
def source
  if @source.is_a?(String)
    @source
  elsif @source.respond_to?(:path)
    @source.path
  elsif @source.respond_to?(:to_path)
    @source.to_path
  else
    @source
  end
end