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


8
9
10
# File 'lib/image_optim/worker/class_methods.rb', line 8

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

Instance Method Details

#bin_symObject

Underscored class name symbol


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

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


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/image_optim/worker/class_methods.rb', line 58

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


42
43
44
45
46
47
48
49
50
51
# File 'lib/image_optim/worker/class_methods.rb', line 42

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


18
19
20
# File 'lib/image_optim/worker/class_methods.rb', line 18

def inherited(base)
  @klasses << base
end

#klassesObject

List of available workers


13
14
15
# File 'lib/image_optim/worker/class_methods.rb', line 13

def klasses
  @klasses.to_enum
end

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


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

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


31
32
33
# File 'lib/image_optim/worker/class_methods.rb', line 31

def option_definitions
  @option_definitions ||= []
end