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



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

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



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

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



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

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)


75
76
77
78
# File 'lib/image_optim/worker.rb', line 75

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

#optionsObject

Return hash with worker options



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

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



66
67
68
69
70
71
72
# File 'lib/image_optim/worker.rb', line 66

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



56
57
58
# File 'lib/image_optim/worker.rb', line 56

def run_order
  0
end

#used_binsObject

List of bins used by worker



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

def used_bins
  [self.class.bin_sym]
end