Module: Pura::Processing::Chainable

Included in:
Builder
Defined in:
lib/pura/processing/chainable.rb

Overview

Implements a chainable interface for building processing options.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Assume that any unknown method names an operation supported by the processor. Add a bang (“!”) if you want processing to be performed.



93
94
95
96
97
98
# File 'lib/pura/processing/chainable.rb', line 93

def method_missing(name, *args, &block)
  return super if name.to_s.end_with?("?")
  return send(name.to_s.chomp("!"), *args, &block).call if name.to_s.end_with?("!")

  operation(name, *args, &block)
end

Instance Method Details

#apply(operations) ⇒ Object

Add multiple operations as a hash or an array.

.apply(resize_to_limit: [400, 400], strip: true)
# or
.apply([[:resize_to_limit, [400, 400]], [:strip, true])


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pura/processing/chainable.rb', line 41

def apply(operations)
  operations.inject(self) do |builder, (name, argument)|
    if argument == true || argument.nil?
      builder.public_send(name)
    elsif argument.is_a?(Array)
      builder.public_send(name, *argument)
    elsif argument.is_a?(Hash)
      builder.public_send(name, **argument)
    else
      builder.public_send(name, argument)
    end
  end
end

#branch(**new_options) ⇒ Object

Creates a new builder object, merging current options with new options.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pura/processing/chainable.rb', line 71

def branch(**new_options)
  options = if is_a?(Builder)
              self.options
            else
              DEFAULT_OPTIONS.merge(processor: self::Processor)
            end

  options = options.merge(new_options) do |key, old_value, new_value|
    case key
    when :loader, :saver then old_value.merge(new_value)
    when :operations     then old_value + new_value
    else                      new_value
    end
  end

  Builder.new(options.freeze)
end

#call(file = nil, destination: nil, **call_options) ⇒ Object

Call the defined processing and get the result. Allows specifying the source file and destination.



62
63
64
65
66
67
68
# File 'lib/pura/processing/chainable.rb', line 62

def call(file = nil, destination: nil, **call_options)
  options = {}
  options[:source] = file if file
  options[:destination] = destination if destination

  branch(**options).call!(**call_options)
end

#convert(format) ⇒ Object

Specify the output format.



17
18
19
# File 'lib/pura/processing/chainable.rb', line 17

def convert(format)
  branch format: format
end

#instrumenter(&block) ⇒ Object

Register instrumentation block that will be called around the pipeline.



32
33
34
# File 'lib/pura/processing/chainable.rb', line 32

def instrumenter(&block)
  branch instrumenter: block
end

#loader(**options) ⇒ Object

Specify processor options applied when loading the image.



22
23
24
# File 'lib/pura/processing/chainable.rb', line 22

def loader(**options)
  branch loader: options
end

#operation(name, *args, &block) ⇒ Object

Add an operation defined by the processor.



56
57
58
# File 'lib/pura/processing/chainable.rb', line 56

def operation(name, *args, &block)
  branch operations: [[name, args, *block]]
end

#saver(**options) ⇒ Object

Specify processor options applied when saving the image.



27
28
29
# File 'lib/pura/processing/chainable.rb', line 27

def saver(**options)
  branch saver: options
end

#source(file) ⇒ Object

Specify the source image file.



12
13
14
# File 'lib/pura/processing/chainable.rb', line 12

def source(file)
  branch source: file
end