Class: ImageOptim::Worker
- Inherits:
-
Object
- Object
- ImageOptim::Worker
- 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
Direct Known Subclasses
Advpng, Gifsicle, Jhead, Jpegoptim, Jpegrecompress, Jpegtran, Optipng, Oxipng, Pngcrush, Pngout, Pngquant, Svgo
Defined Under Namespace
Modules: ClassMethods Classes: Advpng, Gifsicle, Jhead, Jpegoptim, Jpegrecompress, Jpegtran, Optipng, Oxipng, Pngcrush, Pngout, Pngquant, Svgo
Instance Method Summary collapse
-
#image_formats ⇒ Object
List of formats which worker can optimize.
-
#initialize(image_optim, options = {}) ⇒ Worker
constructor
Configure (raises on extra options).
-
#inspect ⇒ Object
Short inspect.
-
#optimize(_src, _dst, options = {}) ⇒ Object
Optimize image at src, output at dst, must be overriden in subclass return true on success.
-
#optimized?(src, dst) ⇒ Boolean
Check if operation resulted in optimized file.
-
#options ⇒ Object
Return hash with worker options.
-
#resolve_used_bins! ⇒ Object
Resolve used bins, raise exception concatenating all messages.
-
#run_order ⇒ Object
Ordering in list of workers, 0 by default.
-
#used_bins ⇒ Object
List of bins used by worker.
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, = {}) unless image_optim.is_a?(ImageOptim) fail ArgumentError, 'first parameter should be an ImageOptim instance' end @image_optim = image_optim () () end |
Instance Method Details
#image_formats ⇒ Object
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 |
#inspect ⇒ Object
Short inspect
86 87 88 89 90 91 |
# File 'lib/image_optim/worker.rb', line 86 def inspect = self.class.option_definitions.map do |option| " @#{option.name}=#{send(option.name).inspect}" end.join(',') "#<#{self.class}#{}>" 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, = {}) fail NotImplementedError, "implement method optimize in #{self.class}" end |
#optimized?(src, dst) ⇒ Boolean
Check if operation resulted in optimized file
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 |
#options ⇒ Object
Return hash with worker options
35 36 37 38 39 40 41 |
# File 'lib/image_optim/worker.rb', line 35 def 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, (errors.join(', ')) end |
#run_order ⇒ Object
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_bins ⇒ Object
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 |