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
- CONFIG_HOME =
File.(ENV['XDG_CONFIG_HOME'] || '~/.config')
- GLOBAL_CONFIG_PATH =
File.join(CONFIG_HOME, 'image_optim.yml')
- LOCAL_CONFIG_PATH =
'./.image_optim.yml'
Class Method Summary collapse
-
.global ⇒ Object
Read config at GLOBAL_CONFIG_PATH if it exists, warn if anything is wrong.
-
.local ⇒ Object
Read config at LOCAL_CONFIG_PATH if it exists, warn if anything is wrong.
Instance Method Summary collapse
- #assert_no_unused_options! ⇒ Object
- #for_worker(klass) ⇒ Object
- #get!(key) ⇒ Object
-
#initialize(options) ⇒ Config
constructor
A new instance of Config.
- #nice ⇒ Object
- #threads ⇒ Object
- #to_s ⇒ Object
- #verbose ⇒ Object
Methods included from OptionHelpers
Constructor Details
#initialize(options) ⇒ Config
Returns a new instance of Config.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/image_optim/config.rb', line 44 def initialize() = [ Config.global, Config.local, HashHelpers.deep_symbolise_keys(), ].reduce do |memo, hash| HashHelpers.deep_merge(memo, hash) end @used = Set.new end |
Class Method Details
.global ⇒ Object
Read config at GLOBAL_CONFIG_PATH if it exists, warn if anything is wrong
20 21 22 |
# File 'lib/image_optim/config.rb', line 20 def global File.file?(GLOBAL_CONFIG_PATH) ? read(GLOBAL_CONFIG_PATH) : {} end |
.local ⇒ Object
Read config at LOCAL_CONFIG_PATH if it exists, warn if anything is wrong
26 27 28 |
# File 'lib/image_optim/config.rb', line 26 def local File.file?(LOCAL_CONFIG_PATH) ? read(LOCAL_CONFIG_PATH) : {} end |
Instance Method Details
#assert_no_unused_options! ⇒ Object
61 62 63 64 65 66 |
# File 'lib/image_optim/config.rb', line 61 def = .reject{ |key, _value| @used.include?(key) } return if .empty? fail ConfigurationError, "unknown options #{unknown_options.inspect} "\ "for #{self}" end |
#for_worker(klass) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/image_optim/config.rb', line 98 def for_worker(klass) = get!(klass.bin_sym) case when Hash when true, nil {} when false false else fail ConfigurationError, "Got #{worker_options.inspect} for "\ "#{klass.name} options" end end |
#get!(key) ⇒ Object
55 56 57 58 59 |
# File 'lib/image_optim/config.rb', line 55 def get!(key) key = key.to_sym @used << key [key] end |
#nice ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/image_optim/config.rb', line 68 def nice nice = get!(:nice) case nice when true, nil 10 when false 0 else nice.to_i end end |
#threads ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/image_optim/config.rb', line 81 def threads threads = get!(:threads) case threads when true, nil processor_count when false 1 else threads.to_i end end |
#to_s ⇒ Object
114 115 116 |
# File 'lib/image_optim/config.rb', line 114 def to_s YAML.dump(HashHelpers.deep_stringify_keys()).sub(/\A---\n/, '') end |
#verbose ⇒ Object
94 95 96 |
# File 'lib/image_optim/config.rb', line 94 def verbose !!get!(:verbose) end |