lib/image_processing/chainable.rb in image_processing-1.11.0 vs lib/image_processing/chainable.rb in image_processing-1.12.0

- old
+ new

@@ -19,10 +19,15 @@ # Specify processor options applied when saving the image. def saver(**options) branch saver: options end + # Register instrumentation block that will be called around the pipeline. + def instrumenter(&block) + branch instrumenter: block + end + # 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]) @@ -38,48 +43,52 @@ builder.send(name, argument) end end end - # Assume that any unknown method names an operation supported by the - # processor. Add a bang ("!") if you want processing to be performed. - 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 - ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true) - # Add an operation defined by the processor. def operation(name, *args, &block) branch operations: [[name, args, *block]] end # Call the defined processing and get the result. Allows specifying # the source file and destination. def call(file = nil, destination: nil, **call_options) - options = {} - options = options.merge(source: file) if file - options = options.merge(destination: destination) if destination + options = { source: file, destination: destination }.compact branch(**options).call!(**call_options) end # Creates a new builder object, merging current options with new options. - def branch(loader: nil, saver: nil, operations: nil, **other_options) - options = respond_to?(:options) ? self.options : DEFAULT_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(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 = 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 - options.freeze + Builder.new(options.freeze) + end - Builder.new(options) + private + + # Assume that any unknown method names an operation supported by the + # processor. Add a bang ("!") if you want processing to be performed. + 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 + ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true) # Empty options which the builder starts with. DEFAULT_OPTIONS = { source: nil, loader: {},