Module: ImageProcessing::Chainable

Included in:
Builder, MiniMagick, Vips
Defined in:
lib/image_processing/chainable.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  source:          nil,
  loader:          {},
  saver:           {},
  format:          nil,
  operations:      [],
  processor_class: nil,
}.freeze

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/image_processing/chainable.rb', line 23

def method_missing(name, *args)
  if name.to_s.end_with?("!")
    send(name.to_s.chomp("!"), *args).call
  elsif name.to_s.end_with?("?")
    super
  else
    operation(name, *args)
  end
end

Instance Method Details

#branch(loader: nil, saver: nil, operations: nil, **other_options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/image_processing/chainable.rb', line 45

def branch(loader: nil, saver: nil, operations: nil, **other_options)
  options = respond_to?(:options) ? self.options : DEFAULT_OPTIONS

  options = options.merge(loader: options[:loader].merge(loader)) if loader
  options = options.merge(saver: options[:saver].merge(saver)) if saver
  options = options.merge(operations: options[:operations] + operations) if operations
  options = options.merge(processor_class: self::Processor) unless self.is_a?(Builder)
  options = options.merge(other_options)

  options.freeze

  Builder.new(options)
end

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



37
38
39
40
41
42
43
# File 'lib/image_processing/chainable.rb', line 37

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

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

#convert(format) ⇒ Object



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

def convert(format)
  branch format: format
end

#custom(&block) ⇒ Object



19
20
21
# File 'lib/image_processing/chainable.rb', line 19

def custom(&block)
  operation :custom, block
end

#loader(**options) ⇒ Object



11
12
13
# File 'lib/image_processing/chainable.rb', line 11

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

#operation(name, *args) ⇒ Object



33
34
35
# File 'lib/image_processing/chainable.rb', line 33

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

#saver(**options) ⇒ Object



15
16
17
# File 'lib/image_processing/chainable.rb', line 15

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

#source(file) ⇒ Object



3
4
5
# File 'lib/image_processing/chainable.rb', line 3

def source(file)
  branch source: file
end