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 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)


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

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


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

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


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

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

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


43
44
45
# File 'lib/image_optim/worker.rb', line 43

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
81
# File 'lib/image_optim/worker.rb', line 78

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

#optionsObject

Return hash with worker options


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

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


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

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


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

def run_order
  0
end

#used_binsObject

List of bins used by worker


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

def used_bins
  [self.class.bin_sym]
end