Sha256: 34f7e761f69dca194fb9b986e1f7f8da74b8135e0456f5e7c11c33ef2768dbc3

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

module ImageProcessing
  # Abstract class inherited by individual processors.
  class Processor
    # Use for processor subclasses to specify the name and the class of their
    # accumulator object (e.g. MiniMagic::Tool or Vips::Image).
    def self.accumulator(name, klass)
      define_method(name) { @accumulator }
      protected(name)
      const_set(:ACCUMULATOR_CLASS, klass)
    end

    # 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.
    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

    def initialize(accumulator = nil)
      @accumulator = accumulator
    end

    # Calls the given block with the accumulator object. Useful for when you
    # want to access the accumulator object directly.
    def custom(&block)
      (block && block.call(@accumulator)) || @accumulator
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
image_processing-1.7.1 lib/image_processing/processor.rb
image_processing-1.7.0 lib/image_processing/processor.rb