Class: ImageOptim::Config

Inherits:
Object
  • Object
show all
Includes:
OptionHelpers
Defined in:
lib/image_optim/config.rb

Overview

Read, merge and parse configuration

Constant Summary collapse

GLOBAL_PATH =

Global config path at ‘$XDG_CONFIG_HOME/image_optim.yml` (by default `~/.config/image_optim.yml`)

File.join(ENV.fetch('XDG_CONFIG_HOME', '~/.config'), 'image_optim.yml')
LOCAL_PATH =

Local config path at ‘./.image_optim.yml`

'./.image_optim.yml'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OptionHelpers

limit_with_range

Constructor Details

#initialize(options) ⇒ Config

Merge config from files with passed options Config files are checked at ‘GLOBAL_PATH` and `LOCAL_PATH` unless overriden using `:config_paths`



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/image_optim/config.rb', line 59

def initialize(options)
  config_paths = options.delete(:config_paths) || [GLOBAL_PATH, LOCAL_PATH]
  config_paths = Array(config_paths)

  to_merge = config_paths.map{ |path| self.class.read_options(path) }
  to_merge << HashHelpers.deep_symbolise_keys(options)

  @options = to_merge.reduce do |memo, hash|
    HashHelpers.deep_merge(memo, hash)
  end
  @used = Set.new
end

Class Method Details

.load_yaml_file(path) ⇒ Object



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

def load_yaml_file(path)
  if YAML.respond_to?(:safe_load_file)
    YAML.safe_load_file(path, permitted_classes: [Range])
  else
    YAML.load_file(path)
  end
end

.read_options(path) ⇒ Object

Read options at path: expand path (warn on failure), return {} if file does not exist or is empty, read yaml, check if it is a Hash, deep symbolise keys



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/image_optim/config.rb', line 27

def read_options(path)
  begin
    full_path = File.expand_path(path)
  rescue ArgumentError => e
    warn "Can't expand path #{path}: #{e}"
    return {}
  end
  return {} unless File.size?(full_path)

  config = load_yaml_file(full_path)
  unless config.is_a?(Hash)
    fail "expected hash, got #{config.inspect}"
  end

  HashHelpers.deep_symbolise_keys(config)
rescue => e
  warn "exception when reading #{full_path}: #{e}"
  {}
end

Instance Method Details

#allow_lossyObject

Allow lossy workers and optimizations, converted to boolean



167
168
169
# File 'lib/image_optim/config.rb', line 167

def allow_lossy
  !!get!(:allow_lossy)
end

#assert_no_unused_options!Object

Fail unless all options were marked as used (directly or indirectly accessed using ‘get!`)



87
88
89
90
91
92
# File 'lib/image_optim/config.rb', line 87

def assert_no_unused_options!
  unknown_options = @options.reject{ |key, _value| @used.include?(key) }
  return if unknown_options.empty?

  fail ConfigurationError, "unknown options #{unknown_options.inspect}"
end

#cache_dirObject



171
172
173
174
# File 'lib/image_optim/config.rb', line 171

def cache_dir
  dir = get!(:cache_dir)
  dir unless dir.nil? || dir.empty?
end

#cache_worker_digestsObject



176
177
178
# File 'lib/image_optim/config.rb', line 176

def cache_worker_digests
  !!get!(:cache_worker_digests)
end

#for_worker(klass) ⇒ Object

Options for worker class by its ‘bin_sym`:

  • ‘Hash` passed as is

  • ‘{}` for `true` or `nil`

  • ‘false` for `false`

  • otherwise fail with ‘ConfigurationError`



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/image_optim/config.rb', line 185

def for_worker(klass)
  worker_options = get!(klass.bin_sym)

  case worker_options
  when Hash
    worker_options
  when true, nil
    {}
  when false
    {disable: true}
  else
    fail ConfigurationError, "Got #{worker_options.inspect} for " \
                             "#{klass.name} options"
  end
end

#get!(key) ⇒ Object

Gets value for key converted to symbol and mark option as used



73
74
75
76
77
# File 'lib/image_optim/config.rb', line 73

def get!(key)
  key = key.to_sym
  @used << key
  @options[key]
end

#key?(key) ⇒ Boolean

Check if key is present

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/image_optim/config.rb', line 80

def key?(key)
  key = key.to_sym
  @options.key?(key)
end

#niceObject

Nice level:

  • ‘10` by default and for `nil` or `true`

  • ‘0` for `false`

  • otherwise convert to integer



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/image_optim/config.rb', line 98

def nice
  nice = get!(:nice)

  case nice
  when true, nil
    10
  when false
    0
  else
    nice.to_i
  end
end

#packObject

Using image_optim_pack:

  • ‘false` to disable

  • ‘nil` to use if available

  • everything else to require



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/image_optim/config.rb', line 145

def pack
  pack = get!(:pack)
  return false if pack == false

  require 'image_optim/pack'
  true
rescue LoadError => e
  raise "Cannot load image_optim_pack: #{e}" if pack

  false
end

#skip_missing_workersObject

Skip missing workers, converted to boolean



158
159
160
161
162
163
164
# File 'lib/image_optim/config.rb', line 158

def skip_missing_workers
  if key?(:skip_missing_workers)
    !!get!(:skip_missing_workers)
  else
    pack
  end
end

#threadsObject

Number of parallel threads:

  • ‘processor_count` by default and for `nil` or `true`

  • ‘1` for `false`

  • otherwise convert to integer



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/image_optim/config.rb', line 115

def threads
  threads = get!(:threads)

  case threads
  when true, nil
    processor_count
  when false
    1
  else
    threads.to_i
  end
end

#timeoutObject

Timeout in seconds for each image:

  • not set by default and for ‘nil`

  • otherwise converted to float



131
132
133
134
# File 'lib/image_optim/config.rb', line 131

def timeout
  timeout = get!(:timeout)
  timeout ? timeout.to_f : nil
end

#to_sObject

yaml dump without document beginning prefix ‘—`



202
203
204
# File 'lib/image_optim/config.rb', line 202

def to_s
  YAML.dump(HashHelpers.deep_stringify_keys(@options)).sub(/\A---\n/, '')
end

#verboseObject

Verbose mode, converted to boolean



137
138
139
# File 'lib/image_optim/config.rb', line 137

def verbose
  !!get!(:verbose)
end