module ImageProcessing::Chainable
def apply(operations)
# or
.apply(resize_to_limit: [400, 400], strip: true)
Add multiple operations as a hash or an array.
def apply(operations) operations.inject(self) do |builder, (name, argument)| if argument == true || argument == nil builder.public_send(name) elsif argument.is_a?(Array) builder.public_send(name, *argument) elsif argument.is_a?(Hash) builder.public_send(name, **argument) else builder.public_send(name, argument) end end end
def branch(**new_options)
def branch(**new_options) if self.is_a?(Builder) options = self.options else options = DEFAULT_OPTIONS.merge(processor: self::Processor) end options = options.merge(new_options) do |key, old_value, new_value| case key when :loader, :saver then old_value.merge(new_value) when :operations then old_value + new_value else new_value end end Builder.new(options.freeze) end
def call(file = nil, destination: nil, **call_options)
Call the defined processing and get the result. Allows specifying
def call(file = nil, destination: nil, **call_options) options = {} options[:source] = file if file options[:destination] = destination if destination branch(**options).call!(**call_options) end
def convert(format)
def convert(format) branch format: format end
def instrumenter(&block)
def instrumenter(&block) branch instrumenter: block end
def loader(**options)
def loader(**options) branch loader: options end
def method_missing(name, *args, &block)
Assume that any unknown method names an operation supported by the
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
def operation(name, *args, &block)
def operation(name, *args, &block) branch operations: [[name, args, *block]] end
def saver(**options)
def saver(**options) branch saver: options end
def source(file)
def source(file) branch source: file end