mixlib-config 2.2.11 → 2.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/lib/mixlib/config.rb +9 -0
- data/lib/mixlib/config/configurable.rb +40 -23
- data/lib/mixlib/config/version.rb +1 -1
- data/spec/mixlib/config_spec.rb +17 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e49d199c25ed417933cc95a2868d154e4f0fbc631aef9d1c5f05310ebbdbbe56
|
4
|
+
data.tar.gz: 4307e6ce5ff263812c96055084d328901587139f82ca213a2ec1272208931150
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdfcc6a831af4bd63216614a7e191152cd5f9dee67e7e87a2775d5d7c766b7c6e8f667ec45062400688e2cc746e2936041087f4e1de85e72f91b10c71c7a36de
|
7
|
+
data.tar.gz: ac0b067a839675c594663b0fa5a91cd66059dc807775dfaacceae62f77efb975d3f26db0ca1781571bfd73c97c5b09bea921fe04fc7e0cf3af48e617917241f5
|
data/README.md
CHANGED
@@ -248,6 +248,18 @@ This allows the user to quickly specify a number of values with one default, whi
|
|
248
248
|
print_network_requests false
|
249
249
|
```
|
250
250
|
|
251
|
+
You can also inspect if the values are still their defaults or not:
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
MyConfig.is_default?(:verbosity) # == true
|
255
|
+
MyConfig[:verbosity] = 5
|
256
|
+
MyConfig.is_default?(:verbosity) # == false
|
257
|
+
MyConfig[:verbosity] = 1
|
258
|
+
MyConfig.is_default?(:verbosity) # == true
|
259
|
+
```
|
260
|
+
|
261
|
+
Trying to call `is_default?` on a config context or a config which does not have a declared default is an error and will raise.
|
262
|
+
|
251
263
|
## Strict Mode
|
252
264
|
|
253
265
|
Misspellings are a common configuration problem, and Mixlib::Config has an answer: `config_strict_mode`. Setting `config_strict_mode` to `true` will cause any misspelled or incorrect configuration option references to throw `Mixlib::Config::UnknownConfigOptionError`.
|
data/lib/mixlib/config.rb
CHANGED
@@ -158,6 +158,15 @@ module Mixlib
|
|
158
158
|
|
159
159
|
alias_method :has_key?, :key?
|
160
160
|
|
161
|
+
def is_default?(key)
|
162
|
+
symbol = key.to_sym
|
163
|
+
if configurables.has_key?(symbol)
|
164
|
+
configurables[symbol].is_default?(configuration)
|
165
|
+
else
|
166
|
+
raise ArgumentError, "config option must exist, and not be a context to check for default values"
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
161
170
|
# Resets a config option to its default.
|
162
171
|
#
|
163
172
|
# === Parameters
|
@@ -1,6 +1,6 @@
|
|
1
1
|
#
|
2
2
|
# Author:: John Keiser (<jkeiser@chef.io>)
|
3
|
-
# Copyright:: Copyright (c) 2013-
|
3
|
+
# Copyright:: Copyright (c) 2013-2018, Chef Software Inc.
|
4
4
|
# License:: Apache License, Version 2.0
|
5
5
|
#
|
6
6
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -19,20 +19,31 @@
|
|
19
19
|
module Mixlib
|
20
20
|
module Config
|
21
21
|
class Configurable
|
22
|
+
attr_reader :symbol
|
23
|
+
attr_reader :default_value
|
24
|
+
attr_reader :default_block
|
25
|
+
|
22
26
|
def initialize(symbol)
|
23
27
|
@symbol = symbol
|
24
|
-
@default_block = nil
|
25
|
-
@has_default = false
|
26
|
-
@default_value = nil
|
27
|
-
@writes_value = nil
|
28
28
|
end
|
29
29
|
|
30
|
-
|
30
|
+
def has_default?
|
31
|
+
instance_variable_defined?(:@default_value)
|
32
|
+
end
|
33
|
+
|
34
|
+
def writes_value?
|
35
|
+
instance_variable_defined?(:@writes_value)
|
36
|
+
end
|
37
|
+
|
38
|
+
def default_block?
|
39
|
+
instance_variable_defined?(:@default_block)
|
40
|
+
end
|
41
|
+
|
42
|
+
alias_method :has_default, :has_default?
|
31
43
|
|
32
44
|
def defaults_to(default_value = nil, &block)
|
33
|
-
@has_default = true
|
34
|
-
@default_block = block
|
35
45
|
@default_value = default_value
|
46
|
+
@default_block = block if block_given?
|
36
47
|
self
|
37
48
|
end
|
38
49
|
|
@@ -42,32 +53,38 @@ module Mixlib
|
|
42
53
|
end
|
43
54
|
|
44
55
|
def get(config)
|
45
|
-
if config.
|
46
|
-
config[
|
47
|
-
elsif
|
48
|
-
|
56
|
+
if config.key?(symbol)
|
57
|
+
config[symbol]
|
58
|
+
elsif default_block?
|
59
|
+
default_block.call
|
49
60
|
else
|
50
|
-
|
51
|
-
# Some things cannot be dup'd, and you won't know this till after the fact
|
52
|
-
# because all values implement dup
|
53
|
-
config[@symbol] = @default_value.dup
|
54
|
-
rescue TypeError
|
55
|
-
@default_value
|
56
|
-
end
|
61
|
+
config[symbol] = safe_dup(default_value)
|
57
62
|
end
|
58
63
|
end
|
59
64
|
|
60
65
|
def set(config, value)
|
61
|
-
config[
|
66
|
+
config[symbol] = writes_value? ? @writes_value.call(value) : value
|
62
67
|
end
|
63
68
|
|
64
69
|
def default
|
65
|
-
if
|
66
|
-
|
70
|
+
if default_block?
|
71
|
+
default_block.call
|
67
72
|
else
|
68
|
-
|
73
|
+
default_value
|
69
74
|
end
|
70
75
|
end
|
76
|
+
|
77
|
+
def is_default?(config)
|
78
|
+
!config.key?(symbol) || config[symbol] == default_value
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def safe_dup(e)
|
84
|
+
e.dup
|
85
|
+
rescue TypeError
|
86
|
+
e
|
87
|
+
end
|
71
88
|
end
|
72
89
|
end
|
73
90
|
end
|
data/spec/mixlib/config_spec.rb
CHANGED
@@ -235,6 +235,23 @@ describe Mixlib::Config do
|
|
235
235
|
expect(@klass.respond_to?("z=".to_sym)).to be false
|
236
236
|
end
|
237
237
|
|
238
|
+
it "returns true for is_default? for a default value" do
|
239
|
+
expect(@klass[:a]).to eql(1)
|
240
|
+
expect(@klass.is_default?(:a)).to be true
|
241
|
+
end
|
242
|
+
|
243
|
+
it "returns true for is_default? for an overwritten default value" do
|
244
|
+
@klass[:a] = 1
|
245
|
+
expect(@klass[:a]).to eql(1)
|
246
|
+
expect(@klass.is_default?(:a)).to be true
|
247
|
+
end
|
248
|
+
|
249
|
+
it "returns false for is_default? for a value that is not the default" do
|
250
|
+
@klass[:a] = 2
|
251
|
+
expect(@klass[:a]).to eql(2)
|
252
|
+
expect(@klass.is_default?(:a)).to be false
|
253
|
+
end
|
254
|
+
|
238
255
|
describe "and extra methods have been dumped into Object" do
|
239
256
|
class NopeError < StandardError
|
240
257
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mixlib-config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chef Software, Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tomlrb
|