Class: ImageProcessing::Pipeline

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

Instance Method Summary collapse

Methods included from Chainable

#branch, #call, #convert, #custom, #default_options, #loader, #method_missing, #operation, #saver, #source

Constructor Details

#initialize(options) ⇒ Pipeline

Returns a new instance of Pipeline.



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

def initialize(options)
  @default_options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ImageProcessing::Chainable

Instance Method Details

#call!(save: true, 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/image_processing/pipeline.rb', line 11

def call!(save: true, destination: nil)
  fail Error, "source file is not provided" unless default_options[:source]

  image_class = default_options[:processor]::IMAGE_CLASS

  if default_options[:source].is_a?(image_class)
    source = default_options[:source]
  elsif default_options[:source].is_a?(String)
    source = default_options[:source]
  elsif default_options[:source].respond_to?(:path)
    source = default_options[:source].path
  elsif default_options[:source].respond_to?(:to_path)
    source = default_options[:source].to_path
  else
    fail Error, "source file needs to respond to #path, or be a String, a Pathname, or a #{image_class} object"
  end

  processor = default_options[:processor].new
  image     = processor.load_image(source, default_options[:loader])

  default_options[:operations].each do |name, args|
    if name == :custom
      image = args.first.call(image) || image
    else
      image = processor.apply_operation(name, image, *args)
    end
  end

  return image unless save

  return processor.save_image(image, destination, default_options[:saver]) if destination

  source_path = source if source.is_a?(String)
  format      = default_options[:format] || File.extname(source_path.to_s)[1..-1] || "jpg"

  result = Tempfile.new(["image_processing", ".#{format}"], binmode: true)

  begin
    processor.save_image(image, result.path, default_options[:saver])
  rescue
    result.close!
    raise
  end

  result.open
  result
end