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

CONFIG_HOME =
File.expand_path(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

Instance Method Summary collapse

Methods included from OptionHelpers

limit_with_range

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(options)
  @options = [
    Config.global,
    Config.local,
    HashHelpers.deep_symbolise_keys(options),
  ].reduce do |memo, hash|
    HashHelpers.deep_merge(memo, hash)
  end
  @used = Set.new
end

Class Method Details

.globalObject

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

.localObject

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 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} "\
      "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)
  worker_options = get!(klass.bin_sym)

  case worker_options
  when Hash
    worker_options
  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
  @options[key]
end

#niceObject



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

#threadsObject



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_sObject



114
115
116
# File 'lib/image_optim/config.rb', line 114

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

#verboseObject



94
95
96
# File 'lib/image_optim/config.rb', line 94

def verbose
  !!get!(:verbose)
end