Module: ImageProcessing::Chainable
- Included in:
- Builder, MiniMagick, Vips
- Defined in:
- lib/image_processing/chainable.rb
Overview
Implements a chainable interface for building processing options.
Constant Summary collapse
- DEFAULT_OPTIONS =
Empty options which the builder starts with.
{ source: nil, loader: {}, saver: {}, format: nil, operations: [], processor: nil, }.freeze
Instance Method Summary collapse
-
#apply(operations) ⇒ Object
Add multiple operations as a hash or an array.
-
#branch(loader: nil, saver: nil, operations: nil, **other_options) ⇒ Object
Creates a new builder object, merging current options with new options.
-
#call(file = nil, destination: nil, **call_options) ⇒ Object
Call the defined processing and get the result.
-
#convert(format) ⇒ Object
Specify the output format.
-
#loader(**options) ⇒ Object
Specify processor options applied when loading the image.
-
#method_missing(name, *args, &block) ⇒ Object
Assume that any unknown method names an operation supported by the processor.
-
#operation(name, *args, &block) ⇒ Object
Add an operation defined by the processor.
-
#saver(**options) ⇒ Object
Specify processor options applied when saving the image.
-
#source(file) ⇒ Object
Specify the source image file.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Assume that any unknown method names an operation supported by the processor. Add a bang (“!”) if you want processing to be performed.
45 46 47 48 49 50 |
# 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 |
Instance Method Details
#apply(operations) ⇒ Object
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])
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# 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) ⇒ Object
Creates a new builder object, merging current options with new options.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/image_processing/chainable.rb', line 69 def branch(loader: nil, saver: nil, operations: nil, **) = respond_to?(:options) ? self. : DEFAULT_OPTIONS = .merge(loader: [:loader].merge(loader)) if loader = .merge(saver: [:saver].merge(saver)) if saver = .merge(operations: [:operations] + operations) if operations = .merge(processor: self::Processor) unless self.is_a?(Builder) = .merge() .freeze Builder.new() end |
#call(file = nil, destination: nil, **call_options) ⇒ Object
Call the defined processing and get the result. Allows specifying the source file and destination.
60 61 62 63 64 65 66 |
# File 'lib/image_processing/chainable.rb', line 60 def call(file = nil, destination: nil, **) = {} = .merge(source: file) if file = .merge(destination: destination) if destination branch(**).call!(**) end |
#convert(format) ⇒ Object
Specify the output format.
10 11 12 |
# File 'lib/image_processing/chainable.rb', line 10 def convert(format) branch format: format end |
#loader(**options) ⇒ Object
Specify processor options applied when loading the image.
15 16 17 |
# File 'lib/image_processing/chainable.rb', line 15 def loader(**) branch loader: end |
#operation(name, *args, &block) ⇒ Object
Add an operation defined by the processor.
54 55 56 |
# File 'lib/image_processing/chainable.rb', line 54 def operation(name, *args, &block) branch operations: [[name, args, *block]] end |
#saver(**options) ⇒ Object
Specify processor options applied when saving the image.
20 21 22 |
# File 'lib/image_processing/chainable.rb', line 20 def saver(**) branch saver: end |
#source(file) ⇒ Object
Specify the source image file.
5 6 7 |
# File 'lib/image_processing/chainable.rb', line 5 def source(file) branch source: file end |