module ImageProcessing::Chainable

Implements a chainable interface for building processing options.

Constants

DEFAULT_OPTIONS

Empty options which the builder starts with.

Public Instance Methods

apply(operations) click to toggle source

Add multiple operations as a hash or an array.

.apply(resize_to_limit: [400, 400], strip: true)
# or
.apply([[:resize_to_limit, [400, 400]], [:strip, true])
# File lib/image_processing/chainable.rb, line 29
def apply(operations)
  operations.inject(self) do |builder, (name, argument)|
    if argument == true || argument == nil
      builder.send(name)
    elsif argument.is_a?(Array)
      builder.send(name, *argument)
    elsif argument.is_a?(Hash)
      builder.send(name, **argument)
    else
      builder.send(name, argument)
    end
  end
end
branch(loader: nil, saver: nil, operations: nil, **other_options) click to toggle source

Creates a new builder object, merging current options with new options.

# File lib/image_processing/chainable.rb, line 69
def branch(loader: nil, saver: nil, operations: nil, **other_options)
  options = respond_to?(:options) ? self.options : DEFAULT_OPTIONS

  options = options.merge(loader: options[:loader].merge(loader)) if loader
  options = options.merge(saver: options[:saver].merge(saver)) if saver
  options = options.merge(operations: options[:operations] + operations) if operations
  options = options.merge(processor: self::Processor) unless self.is_a?(Builder)
  options = options.merge(other_options)

  options.freeze

  Builder.new(options)
end
call(file = nil, destination: nil, **call_options) click to toggle source

Call the defined processing and get the result. Allows specifying the source file and destination.

# File lib/image_processing/chainable.rb, line 60
def call(file = nil, destination: nil, **call_options)
  options = {}
  options = options.merge(source: file) if file
  options = options.merge(destination: destination) if destination

  branch(**options).call!(**call_options)
end
convert(format) click to toggle source

Specify the output format.

# File lib/image_processing/chainable.rb, line 10
def convert(format)
  branch format: format
end
loader(**options) click to toggle source

Specify processor options applied when loading the image.

# File lib/image_processing/chainable.rb, line 15
def loader(**options)
  branch loader: options
end
method_missing(name, *args, &block) click to toggle source

Assume that any unknown method names an operation supported by the processor. Add a bang (“!”) if you want processing to be performed.

Calls superclass method
# File lib/image_processing/chainable.rb, line 45
def method_missing(name, *args, &block)
  return super if name.to_s.end_with?("?")
  return send(name.to_s.chomp("!"), *args, &block).call if name.to_s.end_with?("!")

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

Add an operation defined by the processor.

# File lib/image_processing/chainable.rb, line 54
def operation(name, *args, &block)
  branch operations: [[name, args, *block]]
end
saver(**options) click to toggle source

Specify processor options applied when saving the image.

# File lib/image_processing/chainable.rb, line 20
def saver(**options)
  branch saver: options
end
source(file) click to toggle source

Specify the source image file.

# File lib/image_processing/chainable.rb, line 5
def source(file)
  branch source: file
end