Module: ImageProcessing::Chainable

Included in:
MiniMagick, Pipeline, 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



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

def branch(options)
  options = options.merge(processor: self::Processor) if self.is_a?(Module)
  Pipeline.new(options)
end

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



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

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

  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



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

def default_options
  @default_options ||= {
    source:     nil,
    loader:     {},
    saver:      {},
    format:     nil,
    operations: [],
    processor:  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