Class: ImageOptim::Config

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

Constant Summary collapse

GLOBAL_CONFIG_PATH =
File.join(File.expand_path(ENV['XDG_CONFIG_HOME'] || '~/.config'), '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.



38
39
40
41
42
43
44
45
46
47
# File 'lib/image_optim/config.rb', line 38

def initialize(options)
  @options = [
    Config.global,
    Config.local,
    HashHelpers.deep_symbolise_keys(options),
  ].inject do |memo, hash|
    HashHelpers.deep_merge(memo, hash)
  end
  @used = Set.new
end

Class Method Details

.globalObject



16
17
18
# File 'lib/image_optim/config.rb', line 16

def global
  File.file?(GLOBAL_CONFIG_PATH) ? read(GLOBAL_CONFIG_PATH) : {}
end

.localObject



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 assert_no_unused_options!
  unknown_options = @options.reject{ |key, value| @used.include?(key) }
  unless unknown_options.empty?
    raise ConfigurationError, "unknown options #{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)
  worker_options = get!(klass.bin_sym)

  case worker_options
  when Hash
    worker_options
  when true, nil
    {}
  when false
    false
  else
    raise ConfigurationError, "Got #{worker_options.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

#niceObject



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

#threadsObject



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_sObject



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

#verboseObject



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

def verbose
  !!get!(:verbose)
end