Class: ImageOptim::Worker

Inherits:
Object
  • Object
show all
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/jpegoptim.rb

Direct Known Subclasses

Advpng, Gifsicle, Jhead, Jpegoptim, Jpegtran, Optipng, Pngcrush, Pngout, Svgo

Defined Under Namespace

Classes: Advpng, Gifsicle, Jhead, Jpegoptim, Jpegtran, Optipng, Pngcrush, Pngout, Svgo

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Configure (raises on extra options)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/image_optim/worker.rb', line 36

def initialize(image_optim, options = {})
  @image_optim = image_optim
  self.class.option_definitions.each do |option_definition|
    value = if options.has_key?(option_definition.name)
      options[option_definition.name]
    else
      option_definition.default
    end
    if option_definition.proc
      value = option_definition.proc[value]
    end
    instance_variable_set("@#{option_definition.name}", value)
  end

  assert_no_unknown_options!(options)
end

Class Method Details

.bin_symObject

Underscored class name symbol



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

def bin_sym
  @underscored_name ||= name.split('::').last.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase.to_sym
end

.inherited(base) ⇒ Object

Remember all classes inheriting from this one



16
17
18
# File 'lib/image_optim/worker.rb', line 16

def inherited(base)
  klasses << base
end

.klassesObject

List of available workers



11
12
13
# File 'lib/image_optim/worker.rb', line 11

def klasses
  @klasses ||= []
end

.option(name, default, type, description = nil, &proc) ⇒ Object



29
30
31
32
# File 'lib/image_optim/worker.rb', line 29

def option(name, default, type, description = nil, &proc)
  attr_reader name
  option_definitions << OptionDefinition.new(name, default, type, description, &proc)
end

.option_definitionsObject



25
26
27
# File 'lib/image_optim/worker.rb', line 25

def option_definitions
  @option_definitions ||= []
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  run_order <=> other.run_order
end

#image_formatsObject

List of formats which worker can optimize



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

def image_formats
  format_from_name = self.class.name.downcase[/gif|jpeg|png|svg/]
  raise "#{self.class}: can't guess applicable format from worker name" unless format_from_name
  [format_from_name.to_sym]
end

#optimized?(src, dst) ⇒ Boolean

Check if operation resulted in optimized file

Returns:

  • (Boolean)


70
71
72
# File 'lib/image_optim/worker.rb', line 70

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

#run_orderObject

Ordering in list of workers



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

def run_order
  0
end