Class: ImageProcessing::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/image_processing/processor.rb

Overview

Abstract class inherited by individual processors.

Direct Known Subclasses

MiniMagick::Processor, Vips::Processor

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accumulator = nil) ⇒ Processor

Returns a new instance of Processor.



43
44
45
# File 'lib/image_processing/processor.rb', line 43

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. MiniMagick::Tool or Vips::Image).



24
25
26
27
28
# File 'lib/image_processing/processor.rb', line 24

def self.accumulator(name, klass)
  define_method(name) { @accumulator }
  protected(name)
  const_set(:ACCUMULATOR_CLASS, klass)
end

.apply_operation(accumulator, name, args, block) ⇒ Object

Calls the operation to perform the processing. If the operation is defined on the processor (macro), calls it. Otherwise calls the operation directly on the accumulator object. This provides a common umbrella above defined macros and direct operations.



34
35
36
37
38
39
40
41
# File 'lib/image_processing/processor.rb', line 34

def self.apply_operation(accumulator, (name, args, block))
  if method_defined?(name)
    instance = new(accumulator)
    instance.public_send(name, *args, &block)
  else
    accumulator.send(name, *args, &block)
  end
end

.call(source:, loader:, operations:, saver:, destination: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/image_processing/processor.rb', line 4

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

  accumulator = load_image(source, operations: operations, **loader)

  operations.each do |operation|
    accumulator = apply_operation(accumulator, operation)
  end

  if destination
    save_image(accumulator, destination, **saver)
  else
    accumulator
  end
end

Instance Method Details

#custom(&block) ⇒ Object

Calls the given block with the accumulator object. Useful for when you want to access the accumulator object directly.



49
50
51
# File 'lib/image_processing/processor.rb', line 49

def custom(&block)
  (block && block.call(@accumulator)) || @accumulator
end