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/oxipng.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, Oxipng, Pngcrush, Pngout, Pngquant, Svgo

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
  parse_options(options)
  assert_no_unknown_options!(options)
end

Instance Method Details

#image_formatsObject

List of formats which worker can optimize



50
51
52
53
54
55
56
57
# 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



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

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

#optimize(_src, _dst, options = {}) ⇒ 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, options = {})
  fail NotImplementedError, "implement method optimize in #{self.class}"
end

#optimized?(src, dst) ⇒ Boolean

Check if operation resulted in optimized file

Returns:

  • (Boolean)


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

def optimized?(src, dst)
  dst_size = dst.size?
  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



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

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



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

def run_order
  0
end

#used_binsObject

List of bins used by worker



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

def used_bins
  [self.class.bin_sym]
end