Class: Pura::Processing::Processor
- Inherits:
-
Object
- Object
- Pura::Processing::Processor
- Defined in:
- lib/pura/processing/processor.rb
Overview
Abstract class inherited by individual processors.
Class Method Summary collapse
-
.accumulator(name, klass) ⇒ Object
Use for processor subclasses to specify the name and the class of their accumulator object (e.g. Pura::Image::Wrapper or any backend-specific image class).
-
.apply_operation(accumulator, name, args, block) ⇒ Object
Delegates to #apply_operation.
- .call(source:, loader:, operations:, saver:, destination: nil) ⇒ Object
-
.supports_resize_on_load? ⇒ Boolean
Whether the processor supports resizing the image upon loading.
Instance Method Summary collapse
-
#apply_operation(name, *args, &block) ⇒ Object
Calls the operation to perform the processing.
-
#custom(&block) ⇒ Object
Calls the given block with the accumulator object.
-
#initialize(accumulator = nil) ⇒ Processor
constructor
A new instance of Processor.
Constructor Details
#initialize(accumulator = nil) ⇒ Processor
Returns a new instance of Processor.
55 56 57 |
# File 'lib/pura/processing/processor.rb', line 55 def initialize(accumulator = nil) @accumulator = accumulator end |
Class Method Details
.accumulator(name, klass) ⇒ Object
Use for processor subclasses to specify the name and the class of their accumulator object (e.g. Pura::Image::Wrapper or any backend-specific image class).
39 40 41 42 43 |
# File 'lib/pura/processing/processor.rb', line 39 def self.accumulator(name, klass) define_method(name) { @accumulator } protected(name) const_set(:ACCUMULATOR_CLASS, klass) end |
.apply_operation(accumulator, name, args, block) ⇒ Object
Delegates to #apply_operation.
46 47 48 |
# File 'lib/pura/processing/processor.rb', line 46 def self.apply_operation(accumulator, (name, args, block)) new(accumulator).apply_operation(name, *args, &block) end |
.call(source:, loader:, operations:, saver:, destination: nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pura/processing/processor.rb', line 11 def self.call(source:, loader:, operations:, saver:, destination: nil) unless source.is_a?(String) || source.is_a?(self::ACCUMULATOR_CLASS) fail Error, "invalid source: #{source.inspect}" end if operations.dig(0, 0).to_s.start_with?("resize_") && loader.empty? && supports_resize_on_load? accumulator = source else accumulator = load_image(source, **loader) end operations.each do |operation| accumulator = apply_operation(accumulator, operation) end if destination save_image(accumulator, destination, **saver) else accumulator end end |
.supports_resize_on_load? ⇒ Boolean
Whether the processor supports resizing the image upon loading.
51 52 53 |
# File 'lib/pura/processing/processor.rb', line 51 def self.supports_resize_on_load? false end |
Instance Method Details
#apply_operation(name, *args, &block) ⇒ Object
Calls the operation to perform the processing. If the operation is defined on the processor (macro), calls the method. Otherwise calls the operation directly on the accumulator object. This provides a common umbrella above defined macros and direct operations.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pura/processing/processor.rb', line 63 def apply_operation(name, *args, &block) receiver = respond_to?(name) ? self : @accumulator if args.last.is_a?(Hash) kwargs = args.pop receiver.public_send(name, *args, **kwargs, &block) else receiver.public_send(name, *args, &block) end end |
#custom(&block) ⇒ Object
Calls the given block with the accumulator object. Useful for when you want to access the accumulator object directly.
76 77 78 |
# File 'lib/pura/processing/processor.rb', line 76 def custom(&block) (block && block.call(@accumulator)) || @accumulator end |