class ImageProcessing::Processor

Abstract class inherited by individual processors.

Public Class Methods

accumulator(name, klass) click to toggle source

Use for processor subclasses to specify the name and the class of their accumulator object (e.g. MiniMagick::Tool or Vips::Image).

# File lib/image_processing/processor.rb, line 31
def self.accumulator(name, klass)
  define_method(name) { @accumulator }
  protected(name)
  const_set(:ACCUMULATOR_CLASS, klass)
end
apply_operation(accumulator, (name, args, block)) click to toggle source

Delegates to apply_operation.

# File lib/image_processing/processor.rb, line 38
def self.apply_operation(accumulator, (name, args, block))
  new(accumulator).apply_operation(name, *args, &block)
end
call(source:, loader:, operations:, saver:, destination: nil) click to toggle source
# File lib/image_processing/processor.rb, line 4
def self.call(source:, loader:, operations:, saver:, destination: nil)
  unless source.is_a?(String) || source.is_a?(self::ACCUMULATOR_CLASS)
    fail Error, "invalid source: #{source.inspect}"
  end

  if operations.dig(0, 0).to_s.start_with?("resize_") &&
     loader.empty? &&
     supports_resize_on_load?

    accumulator = source
  else
    accumulator = load_image(source, **loader)
  end

  operations.each do |operation|
    accumulator = apply_operation(accumulator, operation)
  end

  if destination
    save_image(accumulator, destination, **saver)
  else
    accumulator
  end
end
new(accumulator = nil) click to toggle source
# File lib/image_processing/processor.rb, line 47
def initialize(accumulator = nil)
  @accumulator = accumulator
end
supports_resize_on_load?() click to toggle source

Whether the processor supports resizing the image upon loading.

# File lib/image_processing/processor.rb, line 43
def self.supports_resize_on_load?
  false
end

Public Instance Methods

apply_operation(name, *args, &block) click to toggle source

Calls the operation to perform the processing. If the operation is defined on the processor (macro), calls the method. Otherwise calls the operation directly on the accumulator object. This provides a common umbrella above defined macros and direct operations.

# File lib/image_processing/processor.rb, line 55
def apply_operation(name, *args, &block)
  receiver = respond_to?(name) ? self : @accumulator

  if args.last.is_a?(Hash)
    kwargs = args.pop
    receiver.public_send(name, *args, **kwargs, &block)
  else
    receiver.public_send(name, *args, &block)
  end
end
custom(&block) click to toggle source

Calls the given block with the accumulator object. Useful for when you want to access the accumulator object directly.

# File lib/image_processing/processor.rb, line 68
def custom(&block)
  (block && block.call(@accumulator)) || @accumulator
end