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.



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

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



6
7
8
9
10
# File 'lib/image_processing/processor.rb', line 6

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.



16
17
18
19
20
21
22
23
# File 'lib/image_processing/processor.rb', line 16

def self.apply_operation(accumulator, name, *args, &block)
  if (instance_methods - Object.instance_methods).include?(name)
    instance = new(accumulator)
    instance.public_send(name, *args, &block)
  else
    accumulator.send(name, *args, &block)
  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.



31
32
33
# File 'lib/image_processing/processor.rb', line 31

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