Class: ImageOptim::Config
- Inherits:
-
Object
- Object
- ImageOptim::Config
- Includes:
- OptionHelpers
- Defined in:
- lib/image_optim/config.rb
Constant Summary collapse
- GLOBAL_CONFIG_PATH =
File.join(File.(ENV['XDG_CONFIG_HOME'] || '~/.config'), 'image_optim.yml')
- LOCAL_CONFIG_PATH =
'.image_optim.yml'
Class Method Summary collapse
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.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/image_optim/config.rb', line 38 def initialize() @options = [ Config.global, Config.local, HashHelpers.deep_symbolise_keys(), ].inject do |memo, hash| HashHelpers.deep_merge(memo, hash) end @used = Set.new end |
Class Method Details
.global ⇒ Object
16 17 18 |
# File 'lib/image_optim/config.rb', line 16 def global File.file?(GLOBAL_CONFIG_PATH) ? read(GLOBAL_CONFIG_PATH) : {} end |
.local ⇒ Object
20 21 22 |
# File 'lib/image_optim/config.rb', line 20 def local File.file?(LOCAL_CONFIG_PATH) ? read(LOCAL_CONFIG_PATH) : {} end |
Instance Method Details
#assert_no_unused_options! ⇒ Object
55 56 57 58 59 60 |
# File 'lib/image_optim/config.rb', line 55 def = @options.reject{ |key, value| @used.include?(key) } unless .empty? raise ConfigurationError, "unknown options #{.inspect} for #{self}" end end |
#for_worker(klass) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/image_optim/config.rb', line 92 def for_worker(klass) = get!(klass.bin_sym) case when Hash when true, nil {} when false false else raise ConfigurationError, "Got #{.inspect} for #{klass.name} options" end end |
#get!(key) ⇒ Object
49 50 51 52 53 |
# File 'lib/image_optim/config.rb', line 49 def get!(key) key = key.to_sym @used << key @options[key] end |
#nice ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/image_optim/config.rb', line 62 def nice nice = get!(:nice) case nice when true, nil 10 when false 0 else nice.to_i end end |
#threads ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/image_optim/config.rb', line 75 def threads threads = get!(:threads) case threads when true, nil processor_count when false 1 else threads.to_i end end |
#to_s ⇒ Object
107 108 109 |
# File 'lib/image_optim/config.rb', line 107 def to_s YAML.dump(HashHelpers.deep_stringify_keys(@options)).sub(/\A---\n/, '') end |
#verbose ⇒ Object
88 89 90 |
# File 'lib/image_optim/config.rb', line 88 def verbose !!get!(:verbose) end |