Class: ImageOptim::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/image_optim/worker.rb,
lib/image_optim/worker/svgo.rb,
lib/image_optim/worker/jhead.rb,
lib/image_optim/worker/advpng.rb,
lib/image_optim/worker/pngout.rb,
lib/image_optim/worker/optipng.rb,
lib/image_optim/worker/gifsicle.rb,
lib/image_optim/worker/jpegtran.rb,
lib/image_optim/worker/pngcrush.rb,
lib/image_optim/worker/pngquant.rb,
lib/image_optim/worker/jpegoptim.rb

Overview

Base class for all workers

Defined Under Namespace

Classes: Advpng, Gifsicle, Jhead, Jpegoptim, Jpegtran, Optipng, Pngcrush, Pngout, Pngquant, Svgo

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_optim, options = {}) ⇒ Worker

Configure (raises on extra options)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/image_optim/worker.rb', line 43

def initialize(image_optim, options = {})
  unless image_optim.is_a?(ImageOptim)
    fail ArgumentError, 'first parameter should be an ImageOptim instance'
  end
  @image_optim = image_optim
  self.class.option_definitions.each do |option_definition|
    value = if options.key?(option_definition.name)
      options[option_definition.name]
    else
      option_definition.default
    end
    if option_definition.proc
      value = option_definition.proc[value]
    end
    instance_variable_set("@#{option_definition.name}", value)
  end

  assert_no_unknown_options!(options)
end

Class Method Details

.bin_symObject

Underscored class name symbol



24
25
26
27
28
29
# File 'lib/image_optim/worker.rb', line 24

def bin_sym
  @underscored_name ||= name.
    split('::').last. # get last part
    gsub(/([a-z])([A-Z])/, '\1_\2').downcase. # convert AbcDef to abc_def
    to_sym
end

.inherited(base) ⇒ Object

Remember all classes inheriting from this one



19
20
21
# File 'lib/image_optim/worker.rb', line 19

def inherited(base)
  klasses << base
end

.klassesObject

List of available workers



14
15
16
# File 'lib/image_optim/worker.rb', line 14

def klasses
  @klasses ||= []
end

.option(name, default, type, description = nil, &proc) ⇒ Object



35
36
37
38
39
# File 'lib/image_optim/worker.rb', line 35

def option(name, default, type, description = nil, &proc)
  attr_reader name
  option_definitions <<
      OptionDefinition.new(name, default, type, description, &proc)
end

.option_definitionsObject



31
32
33
# File 'lib/image_optim/worker.rb', line 31

def option_definitions
  @option_definitions ||= []
end

Instance Method Details

#<=>(other) ⇒ Object



83
84
85
# File 'lib/image_optim/worker.rb', line 83

def <=>(other)
  run_order <=> other.run_order
end

#image_formatsObject

List of formats which worker can optimize



70
71
72
73
74
75
76
# File 'lib/image_optim/worker.rb', line 70

def image_formats
  format_from_name = self.class.name.downcase[/gif|jpeg|png|svg/]
  unless format_from_name
    fail "#{self.class}: can't guess applicable format from worker name"
  end
  [format_from_name.to_sym]
end

#optimize(_src, _dst) ⇒ Object

Optimize image at src, output at dst, must be overriden in subclass return true on success



65
66
67
# File 'lib/image_optim/worker.rb', line 65

def optimize(_src, _dst)
  fail NotImplementedError, "implement method optimize in #{self.class}"
end

#optimized?(src, dst) ⇒ Boolean

Check if operation resulted in optimized file

Returns:

  • (Boolean)


88
89
90
# File 'lib/image_optim/worker.rb', line 88

def optimized?(src, dst)
  dst.size? && dst.size < src.size
end

#run_orderObject

Ordering in list of workers, 0 by default



79
80
81
# File 'lib/image_optim/worker.rb', line 79

def run_order
  0
end