Module: ImageOptim::Worker::ClassMethods

Included in:
ImageOptim::Worker
Defined in:
lib/image_optim/worker/class_methods.rb

Overview

Class methods of ImageOptim::Worker

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



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

def self.extended(klass)
  klass.instance_variable_set(:@klasses, [])
end

Instance Method Details

#bin_symObject

Underscored class name symbol



26
27
28
29
30
31
32
# File 'lib/image_optim/worker/class_methods.rb', line 26

def bin_sym
  @bin_sym ||=
    name.
    split('::').last. # get last part
    gsub(/([a-z])([A-Z])/, '\1_\2').downcase. # convert AbcDef to abc_def
    to_sym
end

#create_all(image_optim, &options_proc) ⇒ Object

Create list of workers sorted by run order Workers are initialized with options provided through options_proc Resolve all bins of all workers, if there are errors and skip_missing_workers of image_optim is true - show warnings, otherwise fail with one joint exception



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/image_optim/worker/class_methods.rb', line 62

def create_all(image_optim, &options_proc)
  workers = init_all(image_optim, &options_proc)

  resolved = []
  errors = BinResolver.collect_errors(workers) do |worker|
    worker.resolve_used_bins!
    resolved << worker
  end

  unless errors.empty?
    messages = errors.map(&:to_s).uniq
    if image_optim.skip_missing_workers
      messages.each{ |message| warn message }
    else
      joint_message = ['Bin resolving errors:', *messages].join("\n")
      fail BinResolver::Error, joint_message
    end
  end

  resolved.sort_by.with_index{ |worker, i| [worker.run_order, i] }
end

#create_all_by_format(image_optim, &options_proc) ⇒ Object

Create hash with format mapped to list of workers sorted by run order



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

def create_all_by_format(image_optim, &options_proc)
  by_format = {}
  create_all(image_optim, &options_proc).each do |worker|
    worker.image_formats.each do |format|
      by_format[format] ||= []
      by_format[format] << worker
    end
  end
  by_format
end

#inherited(base) ⇒ Object

Remember all classes inheriting from this one



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

def inherited(base)
  super
  @klasses << base
end

#klassesObject

List of available workers



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

def klasses
  @klasses.to_enum
end

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



38
39
40
41
42
43
# File 'lib/image_optim/worker/class_methods.rb', line 38

def option(name, default, type, description = nil, &proc)
  attr_reader name

  OptionDefinition.new(name, default, type, description, &proc).
    tap{ |option_definition| option_definitions << option_definition }
end

#option_definitionsObject



34
35
36
# File 'lib/image_optim/worker/class_methods.rb', line 34

def option_definitions
  @option_definitions ||= []
end