Module: ImageProcessing::Chainable

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/image_processing/chainable.rb', line 30

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(options) ⇒ Object



48
49
50
51
52
# File 'lib/image_processing/chainable.rb', line 48

def branch(options)
  options = options.merge(processor_class: self::Processor) unless self.is_a?(Builder)

  Builder.new(options)
end

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



40
41
42
43
44
45
46
# File 'lib/image_processing/chainable.rb', line 40

def call(file = nil, destination: nil, **call_options)
  options = default_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 default_options.merge(format: format)
end

#custom(&block) ⇒ Object



26
27
28
# File 'lib/image_processing/chainable.rb', line 26

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

#default_optionsObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/image_processing/chainable.rb', line 54

def default_options
  @default_options ||= {
    source:          nil,
    loader:          {},
    saver:           {},
    format:          nil,
    operations:      [],
    processor_class: nil,
  }
end

#loader(**options) ⇒ Object



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

def loader(**options)
  loader = default_options[:loader].merge(options)
  branch default_options.merge(loader: loader)
end

#operation(name, *args) ⇒ Object



21
22
23
24
# File 'lib/image_processing/chainable.rb', line 21

def operation(name, *args)
  operations = default_options[:operations] + [[name, args]]
  branch default_options.merge(operations: operations)
end

#saver(**options) ⇒ Object



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

def saver(**options)
  saver = default_options[:saver].merge(options)
  branch default_options.merge(saver: saver)
end

#source(file) ⇒ Object



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

def source(file)
  branch default_options.merge(source: file)
end