Class: ImageOptim::Worker

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
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,
lib/image_optim/worker/class_methods.rb,
lib/image_optim/worker/jpegrecompress.rb

Overview

Base class for all workers

Defined Under Namespace

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

bin_sym, create_all, create_all_by_format, extended, inherited, klasses, option, option_definitions

Constructor Details

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

Configure (raises on extra options)



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

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
  @allow_lossy = !!options.delete(:allow_lossy)
  parse_options(options)
  assert_no_unknown_options!(options)
end

Instance Attribute Details

#allow_lossyObject (readonly)

Allow lossy optimizations



21
22
23
# File 'lib/image_optim/worker.rb', line 21

def allow_lossy
  @allow_lossy
end

Instance Method Details

#image_formatsObject

List of formats which worker can optimize



50
51
52
53
54
55
56
# File 'lib/image_optim/worker.rb', line 50

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

#inspectObject

Short inspect



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

def inspect
  options_string = options.map do |name, value|
    " @#{name}=#{value.inspect}"
  end.join(',')
  "#<#{self.class}#{options_string}>"
end

#optimize(_src, _dst) ⇒ Object

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



45
46
47
# File 'lib/image_optim/worker.rb', line 45

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)


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

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

#optionsObject

Return hash with worker options



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

def options
  hash = {}
  self.class.option_definitions.each do |option|
    hash[option.name] = send(option.name)
  end
  hash
end

#resolve_used_bins!Object

Resolve used bins, raise exception concatenating all messages



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

def resolve_used_bins!
  errors = BinResolver.collect_errors(used_bins) do |bin|
    @image_optim.resolve_bin!(bin)
  end
  return if errors.empty?
  fail BinResolver::Error, wrap_resolver_error_message(errors.join(', '))
end

#run_orderObject

Ordering in list of workers, 0 by default



59
60
61
# File 'lib/image_optim/worker.rb', line 59

def run_order
  0
end

#used_binsObject

List of bins used by worker



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

def used_bins
  [self.class.bin_sym]
end