Class: ImageProcessing::Pipeline

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

Constant Summary collapse

DEFAULT_FORMAT =
"jpg"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Pipeline

Initializes the pipeline with all the processing options.



10
11
12
13
14
15
# File 'lib/image_processing/pipeline.rb', line 10

def initialize(options)
  options.each do |name, value|
    value = normalize_source(value, options) if name == :source
    instance_variable_set(:"@#{name}", value)
  end
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



7
8
9
# File 'lib/image_processing/pipeline.rb', line 7

def destination
  @destination
end

#formatObject (readonly)

Returns the value of attribute format.



7
8
9
# File 'lib/image_processing/pipeline.rb', line 7

def format
  @format
end

#loaderObject (readonly)

Returns the value of attribute loader.



7
8
9
# File 'lib/image_processing/pipeline.rb', line 7

def loader
  @loader
end

#operationsObject (readonly)

Returns the value of attribute operations.



7
8
9
# File 'lib/image_processing/pipeline.rb', line 7

def operations
  @operations
end

#processorObject (readonly)

Returns the value of attribute processor.



7
8
9
# File 'lib/image_processing/pipeline.rb', line 7

def processor
  @processor
end

#saverObject (readonly)

Returns the value of attribute saver.



7
8
9
# File 'lib/image_processing/pipeline.rb', line 7

def saver
  @saver
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/image_processing/pipeline.rb', line 7

def source
  @source
end

Instance Method Details

#call(save: true) ⇒ Object

Performs the defined series of operations, and saves the result in a new tempfile or a specified path on disk, or if ‘save: false` was passed in returns the unsaved accumulator object that can be used for further processing.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/image_processing/pipeline.rb', line 21

def call(save: true)
  accumulator = processor.load_image(source, **loader)

  operations.each do |name, args, block|
    accumulator = processor.apply_operation(accumulator, name, *args, &block)
  end

  if save == false
    accumulator
  elsif destination
    handle_destination do
      processor.save_image(accumulator, destination, **saver)
    end
  else
    create_tempfile do |tempfile|
      processor.save_image(accumulator, tempfile.path, **saver)
    end
  end
end

#destination_formatObject

Determines the appropriate destination image format.



47
48
49
50
51
52
53
# File 'lib/image_processing/pipeline.rb', line 47

def destination_format
  format   = File.extname(destination)[1..-1] if destination
  format ||= self.format
  format ||= File.extname(source_path)[1..-1] if source_path

  format || DEFAULT_FORMAT
end

#source_pathObject

Retrieves the source path on disk.



42
43
44
# File 'lib/image_processing/pipeline.rb', line 42

def source_path
  source if source.is_a?(String)
end