lib/image_processing/pipeline.rb in image_processing-1.6.0 vs lib/image_processing/pipeline.rb in image_processing-1.7.0
- old
+ new
@@ -4,17 +4,22 @@
class Pipeline
DEFAULT_FORMAT = "jpg"
attr_reader :source, :loader, :saver, :format, :operations, :processor, :destination
+ # Initializes the pipeline with all the processing options.
def initialize(options)
options.each do |name, value|
value = normalize_source(value, options) if name == :source
instance_variable_set(:"@#{name}", value)
end
end
+ # Performs the defined series of operations, and saves the result in a new
+ # tempfile or a specified path on disk, or if `save: false` was passed in
+ # returns the unsaved accumulator object that can be used for further
+ # processing.
def call(save: true)
accumulator = processor.load_image(source, **loader)
operations.each do |name, args, block|
accumulator = processor.apply_operation(accumulator, name, *args, &block)
@@ -31,24 +36,28 @@
processor.save_image(accumulator, tempfile.path, **saver)
end
end
end
+ # Retrieves the source path on disk.
def source_path
source if source.is_a?(String)
end
+ # Determines the appropriate destination image format.
def destination_format
format = File.extname(destination)[1..-1] if destination
format ||= self.format
format ||= File.extname(source_path)[1..-1] if source_path
format || DEFAULT_FORMAT
end
private
+ # Creates a new tempfile for the destination file, yields it, and refreshes
+ # the file descriptor to get the updated file.
def create_tempfile
tempfile = Tempfile.new(["image_processing", ".#{destination_format}"], binmode: true)
yield tempfile
@@ -68,9 +77,10 @@
rescue
File.delete(destination) if File.exist?(destination) && !destination_existed
raise
end
+ # Converts the source image object into a path or the accumulator object.
def normalize_source(source, options)
fail Error, "source file is not provided" unless source
accumulator_class = options[:processor]::ACCUMULATOR_CLASS