lib/image_processing/chainable.rb in image_processing-0.11.2 vs lib/image_processing/chainable.rb in image_processing-1.0.0
- old
+ new
@@ -1,32 +1,25 @@
module ImageProcessing
module Chainable
def source(file)
- branch default_options.merge(source: file)
+ branch source: file
end
def convert(format)
- branch default_options.merge(format: format)
+ branch format: format
end
def loader(**options)
- loader = default_options[:loader].merge(options)
- branch default_options.merge(loader: loader)
+ branch loader: options
end
def saver(**options)
- saver = default_options[:saver].merge(options)
- branch default_options.merge(saver: saver)
+ branch saver: options
end
- def operation(name, *args)
- operations = default_options[:operations] + [[name, args]]
- branch default_options.merge(operations: operations)
- end
-
def custom(&block)
- block ? operation(:custom, block) : self
+ operation :custom, block
end
def method_missing(name, *args)
if name.to_s.end_with?("!")
send(name.to_s.chomp("!"), *args).call
@@ -35,31 +28,41 @@
else
operation(name, *args)
end
end
+ def operation(name, *args)
+ branch operations: [[name, args]]
+ end
+
def call(file = nil, destination: nil, **call_options)
- options = default_options
+ options = {}
options = options.merge(source: file) if file
options = options.merge(destination: destination) if destination
branch(options).call!(**call_options)
end
- def branch(options)
+ 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_class: self::Processor) unless self.is_a?(Builder)
+ options = options.merge(other_options)
+ options.freeze
+
Builder.new(options)
end
- def default_options
- @default_options ||= {
- source: nil,
- loader: {},
- saver: {},
- format: nil,
- operations: [],
- processor_class: nil,
- }
- end
+ DEFAULT_OPTIONS = {
+ source: nil,
+ loader: {},
+ saver: {},
+ format: nil,
+ operations: [],
+ processor_class: nil,
+ }.freeze
end
end