Class: ImageOptim::Config
- Inherits:
-
Object
- Object
- ImageOptim::Config
- 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`)
begin File.join(ENV['XDG_CONFIG_HOME'] || '~/.config', 'image_optim.yml') end
- LOCAL_PATH =
Local config path at ‘./.image_optim.yml`
'./.image_optim.yml'.freeze
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#allow_lossy ⇒ Object
Allow lossy workers and optimizations, converted to boolean.
-
#assert_no_unused_options! ⇒ Object
Fail unless all options were marked as used (directly or indirectly accessed using ‘get!`).
- #cache_dir ⇒ Object
- #cache_worker_digests ⇒ Object
-
#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`.
-
#get!(key) ⇒ Object
Gets value for key converted to symbol and mark option as used.
-
#initialize(options) ⇒ Config
constructor
Merge config from files with passed options Config files are checked at ‘GLOBAL_PATH` and `LOCAL_PATH` unless overriden using `:config_paths`.
-
#key?(key) ⇒ Boolean
Check if key is present.
-
#nice ⇒ Object
Nice level: * ‘10` by default and for `nil` or `true` * `0` for `false` * otherwise convert to integer.
-
#pack ⇒ Object
Using image_optim_pack: * ‘false` to disable * `nil` to use if available * everything else to require.
-
#skip_missing_workers ⇒ Object
Skip missing workers, converted to boolean.
-
#threads ⇒ Object
Number of parallel threads: * ‘processor_count` by default and for `nil` or `true` * `1` for `false` * otherwise convert to integer.
- #timeout ⇒ Object
-
#to_s ⇒ Object
yaml dump without document beginning prefix ‘—`.
-
#verbose ⇒ Object
Verbose mode, converted to boolean.
Methods included from OptionHelpers
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`
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/image_optim/config.rb', line 49 def initialize() config_paths = .delete(:config_paths) || [GLOBAL_PATH, LOCAL_PATH] config_paths = Array(config_paths) to_merge = config_paths.map{ |path| self.class.(path) } to_merge << HashHelpers.deep_symbolise_keys() = to_merge.reduce do |memo, hash| HashHelpers.deep_merge(memo, hash) end @used = Set.new end |
Class Method Details
.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 |
# File 'lib/image_optim/config.rb', line 27 def (path) begin full_path = File.(path) rescue ArgumentError => e warn "Can't expand path #{path}: #{e}" return {} end return {} unless File.size?(full_path) config = YAML.load_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_lossy ⇒ Object
Allow lossy workers and optimizations, converted to boolean
147 148 149 |
# File 'lib/image_optim/config.rb', line 147 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!`)
77 78 79 80 81 |
# File 'lib/image_optim/config.rb', line 77 def = .reject{ |key, _value| @used.include?(key) } return if .empty? fail ConfigurationError, "unknown options #{unknown_options.inspect}" end |
#cache_dir ⇒ Object
151 152 153 154 |
# File 'lib/image_optim/config.rb', line 151 def cache_dir dir = get!(:cache_dir) dir unless dir.nil? || dir.empty? end |
#cache_worker_digests ⇒ Object
156 157 158 |
# File 'lib/image_optim/config.rb', line 156 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`
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/image_optim/config.rb', line 170 def for_worker(klass) = get!(klass.bin_sym) case when Hash 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
63 64 65 66 67 |
# File 'lib/image_optim/config.rb', line 63 def get!(key) key = key.to_sym @used << key [key] end |
#key?(key) ⇒ Boolean
Check if key is present
70 71 72 73 |
# File 'lib/image_optim/config.rb', line 70 def key?(key) key = key.to_sym .key?(key) end |
#nice ⇒ Object
Nice level:
-
‘10` by default and for `nil` or `true`
-
‘0` for `false`
-
otherwise convert to integer
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/image_optim/config.rb', line 87 def nice nice = get!(:nice) case nice when true, nil 10 when false 0 else nice.to_i end end |
#pack ⇒ Object
Using image_optim_pack:
-
‘false` to disable
-
‘nil` to use if available
-
everything else to require
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/image_optim/config.rb', line 126 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_workers ⇒ Object
Skip missing workers, converted to boolean
138 139 140 141 142 143 144 |
# File 'lib/image_optim/config.rb', line 138 def skip_missing_workers if key?(:skip_missing_workers) !!get!(:skip_missing_workers) else pack end end |
#threads ⇒ Object
Number of parallel threads:
-
‘processor_count` by default and for `nil` or `true`
-
‘1` for `false`
-
otherwise convert to integer
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/image_optim/config.rb', line 104 def threads threads = get!(:threads) case threads when true, nil processor_count when false 1 else threads.to_i end end |
#timeout ⇒ Object
160 161 162 163 |
# File 'lib/image_optim/config.rb', line 160 def timeout timeout = get!(:timeout) timeout ? timeout.to_i : 0 end |
#to_s ⇒ Object
yaml dump without document beginning prefix ‘—`
187 188 189 |
# File 'lib/image_optim/config.rb', line 187 def to_s YAML.dump(HashHelpers.deep_stringify_keys()).sub(/\A---\n/, '') end |
#verbose ⇒ Object
Verbose mode, converted to boolean
118 119 120 |
# File 'lib/image_optim/config.rb', line 118 def verbose !!get!(:verbose) end |