mixlib-config 2.2.12 → 3.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e49d199c25ed417933cc95a2868d154e4f0fbc631aef9d1c5f05310ebbdbbe56
4
- data.tar.gz: 4307e6ce5ff263812c96055084d328901587139f82ca213a2ec1272208931150
3
+ metadata.gz: 6997a320d19a4e3844ff8ccdb84dbf5a830dc7b83114e787a413cc4cc64c3776
4
+ data.tar.gz: 379b34bd06770b8e24f14eb3060577794d5e3f4fc2a5ecc5d25df951eefec632
5
5
  SHA512:
6
- metadata.gz: cdfcc6a831af4bd63216614a7e191152cd5f9dee67e7e87a2775d5d7c766b7c6e8f667ec45062400688e2cc746e2936041087f4e1de85e72f91b10c71c7a36de
7
- data.tar.gz: ac0b067a839675c594663b0fa5a91cd66059dc807775dfaacceae62f77efb975d3f26db0ca1781571bfd73c97c5b09bea921fe04fc7e0cf3af48e617917241f5
6
+ metadata.gz: 776bf8d4cf896adb8164cc73ba2d7e44c9ebc343fdbca6d78084976ef69a8f50add631bff91a33d36a785df04243357ddd1959619a392919dbdd24c7f8dbed83
7
+ data.tar.gz: adc0991e31c17d3d485a694a4bc9e74c79ed41465cedd59d1229995d52bee49ae5e15fb6705fb89f4581ed3bad01e77556c898f38a15cba0055acd96a4f5745d
data/lib/mixlib/config.rb CHANGED
@@ -18,11 +18,11 @@
18
18
  # limitations under the License.
19
19
  #
20
20
 
21
- require "mixlib/config/version"
22
- require "mixlib/config/configurable"
23
- require "mixlib/config/unknown_config_option_error"
24
- require "mixlib/config/reopened_config_context_with_configurable_error"
25
- require "mixlib/config/reopened_configurable_with_config_context_error"
21
+ require_relative "config/version"
22
+ require_relative "config/configurable"
23
+ require_relative "config/unknown_config_option_error"
24
+ require_relative "config/reopened_config_context_with_configurable_error"
25
+ require_relative "config/reopened_configurable_with_config_context_error"
26
26
 
27
27
  module Mixlib
28
28
  module Config
@@ -33,11 +33,11 @@ module Mixlib
33
33
  class << base; attr_accessor :config_context_lists; end
34
34
  class << base; attr_accessor :config_context_hashes; end
35
35
  class << base; attr_accessor :config_parent; end
36
- base.configuration = Hash.new
37
- base.configurables = Hash.new
38
- base.config_contexts = Hash.new
39
- base.config_context_lists = Hash.new
40
- base.config_context_hashes = Hash.new
36
+ base.configuration = ({})
37
+ base.configurables = ({})
38
+ base.config_contexts = ({})
39
+ base.config_context_lists = ({})
40
+ base.config_context_hashes = ({})
41
41
  base.initialize_mixlib_config
42
42
  end
43
43
 
@@ -70,7 +70,7 @@ module Mixlib
70
70
  # filename<String>:: A filename to read from
71
71
  def from_yaml(filename)
72
72
  require "yaml"
73
- from_hash(YAML.load(IO.read(filename)), filename)
73
+ from_hash(YAML.load(IO.read(filename)))
74
74
  end
75
75
 
76
76
  # Parses valid JSON structure into Ruby
@@ -79,7 +79,7 @@ module Mixlib
79
79
  # filename<String>:: A filename to read from
80
80
  def from_json(filename)
81
81
  require "json"
82
- from_hash(JSON.parse(IO.read(filename)), filename)
82
+ from_hash(JSON.parse(IO.read(filename)))
83
83
  end
84
84
 
85
85
  def from_toml(filename)
@@ -91,20 +91,8 @@ module Mixlib
91
91
  #
92
92
  # === Parameters
93
93
  # hash<Hash>:: A Hash containing configuration
94
- def from_hash(hash, filename = "in_memory")
95
- ruby_translation = []
96
-
97
- to_dotted_hash(hash).each do |k, v|
98
- if v.is_a? Array
99
- ruby_translation << "#{k} #{v}"
100
- elsif v.is_a? String
101
- ruby_translation << "#{k} \"#{v}\""
102
- else
103
- ruby_translation << "#{k} #{v}"
104
- end
105
- end
106
-
107
- instance_eval(ruby_translation.join("\n"), filename, 1)
94
+ def from_hash(hash)
95
+ apply_nested_hash(hash)
108
96
  end
109
97
 
110
98
  # Pass Mixlib::Config.configure() a block, and it will yield itself
@@ -153,14 +141,14 @@ module Mixlib
153
141
  # <True>:: If the config option exists
154
142
  # <False>:: If the config option does not exist
155
143
  def key?(key)
156
- configuration.has_key?(key.to_sym) || config_contexts.has_key?(key.to_sym)
144
+ configuration.key?(key.to_sym) || config_contexts.key?(key.to_sym)
157
145
  end
158
146
 
159
147
  alias_method :has_key?, :key?
160
148
 
161
149
  def is_default?(key)
162
150
  symbol = key.to_sym
163
- if configurables.has_key?(symbol)
151
+ if configurables.key?(symbol)
164
152
  configurables[symbol].is_default?(configuration)
165
153
  else
166
154
  raise ArgumentError, "config option must exist, and not be a context to check for default values"
@@ -177,8 +165,8 @@ module Mixlib
177
165
 
178
166
  # Resets all config options to their defaults.
179
167
  def reset
180
- self.configuration = Hash.new
181
- config_contexts.values.each { |config_context| config_context.reset }
168
+ self.configuration = ({})
169
+ config_contexts.values.each(&:reset)
182
170
  end
183
171
 
184
172
  # Makes a copy of any non-default values.
@@ -262,9 +250,9 @@ module Mixlib
262
250
  # === Returns
263
251
  # self
264
252
  def restore(hash)
265
- self.configuration = hash.reject { |key, value| config_contexts.has_key?(key) }
253
+ self.configuration = hash.reject { |key, value| config_contexts.key?(key) }
266
254
  config_contexts.each do |key, config_context|
267
- if hash.has_key?(key)
255
+ if hash.key?(key)
268
256
  config_context.restore(hash[key])
269
257
  else
270
258
  config_context.reset
@@ -272,7 +260,7 @@ module Mixlib
272
260
  end
273
261
  config_context_lists.each do |key, meta|
274
262
  meta[:values] = []
275
- if hash.has_key?(key)
263
+ if hash.key?(key)
276
264
  hash[key].each do |val|
277
265
  context = define_context(meta[:definition_blocks])
278
266
  context.restore(val)
@@ -282,7 +270,7 @@ module Mixlib
282
270
  end
283
271
  config_context_hashes.each do |key, meta|
284
272
  meta[:values] = {}
285
- if hash.has_key?(key)
273
+ if hash.key?(key)
286
274
  hash[key].each do |vkey, val|
287
275
  context = define_context(meta[:definition_blocks])
288
276
  context.restore(val)
@@ -301,7 +289,7 @@ module Mixlib
301
289
  # self
302
290
  def merge!(hash)
303
291
  hash.each do |key, value|
304
- if config_contexts.has_key?(key)
292
+ if config_contexts.key?(key)
305
293
  # Grab the config context and let internal_get cache it if so desired
306
294
  config_contexts[key].restore(value)
307
295
  else
@@ -381,9 +369,10 @@ module Mixlib
381
369
  # The value of the config option.
382
370
  def configurable(symbol, &block)
383
371
  unless configurables[symbol]
384
- if config_contexts.has_key?(symbol)
372
+ if config_contexts.key?(symbol)
385
373
  raise ReopenedConfigContextWithConfigurableError, "Cannot redefine config_context #{symbol} as a configurable value"
386
374
  end
375
+
387
376
  configurables[symbol] = Configurable.new(symbol)
388
377
  define_attr_accessor_methods(symbol)
389
378
  end
@@ -409,11 +398,11 @@ module Mixlib
409
398
  # block<Block>: a block that will be run in the context of this new config
410
399
  # class.
411
400
  def config_context(symbol, &block)
412
- if configurables.has_key?(symbol)
401
+ if configurables.key?(symbol)
413
402
  raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{symbol} with a config context"
414
403
  end
415
404
 
416
- if config_contexts.has_key?(symbol)
405
+ if config_contexts.key?(symbol)
417
406
  context = config_contexts[symbol]
418
407
  else
419
408
  context = Class.new
@@ -447,11 +436,11 @@ module Mixlib
447
436
  # block<Block>: a block that will be run in the context of this new config
448
437
  # class.
449
438
  def config_context_list(plural_symbol, singular_symbol, &block)
450
- if configurables.has_key?(plural_symbol)
439
+ if configurables.key?(plural_symbol)
451
440
  raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{plural_symbol} with a config context"
452
441
  end
453
442
 
454
- unless config_context_lists.has_key?(plural_symbol)
443
+ unless config_context_lists.key?(plural_symbol)
455
444
  config_context_lists[plural_symbol] = {
456
445
  definition_blocks: [],
457
446
  values: [],
@@ -479,11 +468,11 @@ module Mixlib
479
468
  # block<Block>: a block that will be run in the context of this new config
480
469
  # class.
481
470
  def config_context_hash(plural_symbol, singular_symbol, &block)
482
- if configurables.has_key?(plural_symbol)
471
+ if configurables.key?(plural_symbol)
483
472
  raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{plural_symbol} with a config context"
484
473
  end
485
474
 
486
- unless config_context_hashes.has_key?(plural_symbol)
475
+ unless config_context_hashes.key?(plural_symbol)
487
476
  config_context_hashes[plural_symbol] = {
488
477
  definition_blocks: [],
489
478
  values: {},
@@ -543,9 +532,10 @@ module Mixlib
543
532
  # <ArgumentError>:: if value is set to something other than true, false, or :warn
544
533
  #
545
534
  def config_strict_mode=(value)
546
- if ![ true, false, :warn, nil ].include?(value)
535
+ unless [ true, false, :warn, nil ].include?(value)
547
536
  raise ArgumentError, "config_strict_mode must be true, false, nil or :warn"
548
537
  end
538
+
549
539
  @config_strict_mode = value
550
540
  end
551
541
 
@@ -567,6 +557,28 @@ module Mixlib
567
557
  internal_get_or_set(method_symbol, *args)
568
558
  end
569
559
 
560
+ protected
561
+
562
+ # Given a (nested) Hash, apply it to the config object and any contexts.
563
+ #
564
+ # This is preferable to converting it to the string representation with
565
+ # the #to_dotted_hash method above.
566
+ #
567
+ # === Parameters
568
+ # hash<Hash>:: The hash to apply to the config oject
569
+ def apply_nested_hash(hash)
570
+ hash.each do |k, v|
571
+ if v.is_a? Hash
572
+ # If loading from hash, and we reference a context that doesn't exist
573
+ # and warning/strict is off, we need to create the config context that we expected to be here.
574
+ context = internal_get(k.to_sym) || config_context(k.to_sym)
575
+ context.apply_nested_hash(v)
576
+ else
577
+ internal_set(k.to_sym, v)
578
+ end
579
+ end
580
+ end
581
+
570
582
  private
571
583
 
572
584
  # Given a (nested) Hash, turn it into a single top-level hash using dots as
@@ -597,9 +609,9 @@ module Mixlib
597
609
  # value<Object>:: Value to be set in config hash
598
610
  #
599
611
  def internal_set(symbol, value)
600
- if configurables.has_key?(symbol)
612
+ if configurables.key?(symbol)
601
613
  configurables[symbol].set(configuration, value)
602
- elsif config_contexts.has_key?(symbol)
614
+ elsif config_contexts.key?(symbol)
603
615
  config_contexts[symbol].restore(value.to_hash)
604
616
  else
605
617
  if config_strict_mode == :warn
@@ -612,13 +624,13 @@ module Mixlib
612
624
  end
613
625
 
614
626
  def internal_get(symbol)
615
- if configurables.has_key?(symbol)
627
+ if configurables.key?(symbol)
616
628
  configurables[symbol].get(configuration)
617
- elsif config_contexts.has_key?(symbol)
629
+ elsif config_contexts.key?(symbol)
618
630
  config_contexts[symbol]
619
- elsif config_context_lists.has_key?(symbol)
631
+ elsif config_context_lists.key?(symbol)
620
632
  config_context_lists[symbol]
621
- elsif config_context_hashes.has_key?(symbol)
633
+ elsif config_context_hashes.key?(symbol)
622
634
  config_context_hashes[symbol]
623
635
  else
624
636
  if config_strict_mode == :warn
@@ -695,7 +707,7 @@ module Mixlib
695
707
  # Adds a single new context to the list
696
708
  meta.send :define_method, singular_symbol do |key, &block|
697
709
  context_hash_details = internal_get(plural_symbol)
698
- context = if context_hash_details[:values].has_key? key
710
+ context = if context_hash_details[:values].key? key
699
711
  context_hash_details[:values][key]
700
712
  else
701
713
  new_context = define_context(context_hash_details[:definition_blocks])
@@ -19,7 +19,7 @@
19
19
  module Mixlib
20
20
  module Config
21
21
 
22
- VERSION = "2.2.12"
22
+ VERSION = "3.0.6".freeze
23
23
 
24
24
  end
25
25
  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.12
4
+ version: 3.0.6
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-06 00:00:00.000000000 Z
11
+ date: 2019-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tomlrb
@@ -24,85 +24,21 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rspec
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '3.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '3.0'
55
- - !ruby/object:Gem::Dependency
56
- name: chefstyle
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: rdoc
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
27
  description: A class based configuration library
84
- email: legal@chef.io
28
+ email: info@chef.io
85
29
  executables: []
86
30
  extensions: []
87
- extra_rdoc_files:
88
- - LICENSE
89
- - README.md
31
+ extra_rdoc_files: []
90
32
  files:
91
- - Gemfile
92
33
  - LICENSE
93
34
  - NOTICE
94
- - README.md
95
- - Rakefile
96
35
  - lib/mixlib/config.rb
97
36
  - lib/mixlib/config/configurable.rb
98
37
  - lib/mixlib/config/reopened_config_context_with_configurable_error.rb
99
38
  - lib/mixlib/config/reopened_configurable_with_config_context_error.rb
100
39
  - lib/mixlib/config/unknown_config_option_error.rb
101
40
  - lib/mixlib/config/version.rb
102
- - mixlib-config.gemspec
103
- - spec/mixlib/config_spec.rb
104
- - spec/spec_helper.rb
105
- homepage: https://www.chef.io
41
+ homepage: https://github.com/chef/mixlib-config
106
42
  licenses:
107
43
  - Apache-2.0
108
44
  metadata: {}
@@ -114,15 +50,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
50
  requirements:
115
51
  - - ">="
116
52
  - !ruby/object:Gem::Version
117
- version: '2.2'
53
+ version: '2.4'
118
54
  required_rubygems_version: !ruby/object:Gem::Requirement
119
55
  requirements:
120
56
  - - ">="
121
57
  - !ruby/object:Gem::Version
122
58
  version: '0'
123
59
  requirements: []
124
- rubyforge_project:
125
- rubygems_version: 2.7.6
60
+ rubygems_version: 3.0.3
126
61
  signing_key:
127
62
  specification_version: 4
128
63
  summary: A class based configuration library
data/Gemfile DELETED
@@ -1,3 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gemspec
data/README.md DELETED
@@ -1,316 +0,0 @@
1
- # Mixlib::Config
2
-
3
- [![Build Status](https://travis-ci.org/chef/mixlib-config.svg?branch=master)](https://travis-ci.org/chef/mixlib-config)[![Gem Version](https://badge.fury.io/rb/mixlib-config.svg)](https://badge.fury.io/rb/mixlib-config)
4
-
5
- Mixlib::Config provides a class-based configuration object, as used in Chef. To use in your project:
6
-
7
- ```ruby
8
- require 'mixlib/config'
9
-
10
- module MyConfig
11
- extend Mixlib::Config
12
- config_strict_mode true
13
- default :first_value, 'something'
14
- default :other_value, 'something_else'
15
- end
16
- ```
17
-
18
- You can use this to provide a configuration file for a user. For example, if you do this:
19
-
20
- ```ruby
21
- MyConfig.from_file('~/.myconfig.rb')
22
- ```
23
-
24
- A user could write a Ruby config file that looked like this:
25
-
26
- ```ruby
27
- first_value 'hi'
28
- second_value "#{first_value}! 10 times 10 is #{10*10}!"
29
- ```
30
-
31
- Inside your app, you can check configuration values with this syntax:
32
-
33
- ```ruby
34
- MyConfig.first_value # returns 'something'
35
- MyConfig[:first_value] # returns 'something'
36
- ```
37
-
38
- And you can modify configuration values with this syntax:
39
-
40
- ```ruby
41
- MyConfig.first_value('foobar') # sets first_value to 'foobar'
42
- MyConfig.first_value = 'foobar' # sets first_value to 'foobar'
43
- MyConfig[:first_value] = 'foobar' # sets first_value to 'foobar'
44
- ```
45
-
46
- If you prefer to allow your users to pass in configuration via YAML, JSON or TOML files, `mixlib-config` supports that too!
47
-
48
- ```ruby
49
- MyConfig.from_file('~/.myconfig.yml')
50
- MyConfig.from_file('~/.myconfig.json')
51
- MyConfig.from_file('~/.myconfig.toml')
52
- ```
53
-
54
- This way, a user could write a YAML config file that looked like this:
55
-
56
- ```yaml
57
- ---
58
- first_value: 'hi'
59
- second_value: 'goodbye'
60
- ```
61
-
62
- or a JSON file that looks like this:
63
-
64
- ```json
65
- {
66
- "first_value": "hi",
67
- "second_value": "goodbye"
68
- }
69
- ```
70
-
71
- or a TOML file that looks like this:
72
-
73
- ```toml
74
- first_value = "hi"
75
- second_value = "goodbye"
76
- ```
77
-
78
- Please note: There is an inherent limitation in the logic you can do with YAML and JSON file. At this time, `mixlib-config` does not support ERB or other logic in YAML or JSON config (read "static content only").
79
-
80
- ## Nested Configuration
81
-
82
- Often you want to be able to group configuration options to provide a common context. Mixlib::Config supports this thus:
83
-
84
- ```ruby
85
- require 'mixlib/config'
86
-
87
- module MyConfig
88
- extend Mixlib::Config
89
- config_context :logging do
90
- default :base_filename, 'mylog'
91
- default :max_log_files, 10
92
- end
93
- end
94
- ```
95
-
96
- The user can write their config file in one of three formats:
97
-
98
- ### Method Style
99
-
100
- ```ruby
101
- logging.base_filename 'superlog'
102
- logging.max_log_files 2
103
- ```
104
-
105
- ### Block Style
106
-
107
- Using this format the block is executed in the context, so all configurables on that context is directly accessible
108
-
109
- ```ruby
110
- logging do
111
- base_filename 'superlog'
112
- max_log_files 2
113
- end
114
- ```
115
-
116
- ### Block with Argument Style
117
-
118
- Using this format the context is given to the block as an argument
119
-
120
- ```ruby
121
- logging do |l|
122
- l.base_filename = 'superlog'
123
- l.max_log_files = 2
124
- end
125
- ```
126
-
127
- You can access these variables thus:
128
-
129
- ```ruby
130
- MyConfig.logging.base_filename
131
- MyConfig[:logging][:max_log_files]
132
- ```
133
-
134
- ### Lists of Contexts
135
- For use cases where you need to be able to specify a list of things with identical configuration
136
- you can define a `context_config_list` like so:
137
-
138
- ```ruby
139
- require 'mixlib/config'
140
-
141
- module MyConfig
142
- extend Mixlib::Config
143
-
144
- # The first argument is the plural word for your item, the second is the singular
145
- config_context_list :apples, :apple do
146
- default :species
147
- default :color, 'red'
148
- default :crispness, 10
149
- end
150
- end
151
- ```
152
-
153
- With this definition everytime the `apple` is called within the config file it
154
- will create a new item that can be configured with a block like so:
155
-
156
- ```ruby
157
- apple do
158
- species 'Royal Gala'
159
- end
160
- apple do
161
- species 'Granny Smith'
162
- color 'green'
163
- end
164
- ```
165
-
166
- You can then iterate over the defined values in code:
167
-
168
- ```ruby
169
- MyConfig.apples.each do |apple|
170
- puts "#{apple.species} are #{apple.color}"
171
- end
172
-
173
- # => Royal Gala are red
174
- # => Granny Smith are green
175
- ```
176
-
177
- _**Note**: When using the config context lists they must use the [block style](#block-style) or [block with argument style](#block-with-argument-style)_
178
-
179
- ### Hashes of Contexts
180
- For use cases where you need to be able to specify a list of things with identical configuration
181
- that are keyed to a specific value, you can define a `context_config_hash` like so:
182
-
183
- ```ruby
184
- require 'mixlib/config'
185
-
186
- module MyConfig
187
- extend Mixlib::Config
188
-
189
- # The first argument is the plural word for your item, the second is the singular
190
- config_context_hash :apples, :apple do
191
- default :species
192
- default :color, 'red'
193
- default :crispness, 10
194
- end
195
- end
196
- ```
197
-
198
- This can then be used in the config file like so:
199
-
200
- ```ruby
201
- apple 'Royal Gala' do
202
- species 'Royal Gala'
203
- end
204
- apple 'Granny Smith' do
205
- species 'Granny Smith'
206
- color 'green'
207
- end
208
-
209
- # You can also reopen a context to edit a value
210
- apple 'Royal Gala' do
211
- crispness 3
212
- end
213
- ```
214
-
215
- You can then iterate over the defined values in code:
216
-
217
- ```ruby
218
- MyConfig.apples.each do |key, apple|
219
- puts "#{key} => #{apple.species} are #{apple.color}"
220
- end
221
-
222
- # => Royal Gala => Royal Gala are red
223
- # => Granny Smith => Granny Smith are green
224
- ```
225
-
226
- _**Note**: When using the config context hashes they must use the [block style](#block-style) or [block with argument style](#block-with-argument-style)_
227
-
228
- ## Default Values
229
-
230
- Mixlib::Config has a powerful default value facility. In addition to being able to specify explicit default values, you can even specify Ruby code blocks that will run if the config value is not set. This can allow you to build options whose values are based on other options.
231
-
232
- ```ruby
233
- require 'mixlib/config'
234
-
235
- module MyConfig
236
- extend Mixlib::Config
237
- config_strict_mode true
238
- default :verbosity, 1
239
- default(:print_network_requests) { verbosity >= 2 }
240
- default(:print_ridiculously_unimportant_stuff) { verbosity >= 10 }
241
- end
242
- ```
243
-
244
- This allows the user to quickly specify a number of values with one default, while still allowing them to override anything:
245
-
246
- ```ruby
247
- verbosity 5
248
- print_network_requests false
249
- ```
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
-
263
- ## Strict Mode
264
-
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`.
266
-
267
- ```ruby
268
- require 'mixlib/config'
269
-
270
- module MyConfig
271
- extend Mixlib::Config
272
- config_strict_mode true
273
- default :filename, '~/output.txt'
274
- configurable :server_url # configurable declares an option with no default value
275
- config_context :logging do
276
- default :base_name, 'log'
277
- default :max_files, 20
278
- end
279
- end
280
- ```
281
-
282
- Now if a user types `fielname "~/output-mine.txt"` in their configuration file, it will toss an exception telling them that the option "fielname" is unknown. If you do not set config_strict_mode, the fielname option will be merrily set and the application just won't know about it.
283
-
284
- Different config_contexts can have different strict modes; but they inherit the strict mode of their parent if you don't explicitly set it. So setting it once at the top level is sufficient. In the above example, `logging.base_naem 'mylog'` will raise an error.
285
-
286
- In conclusion: _always set config_strict_mode to true_. You know you want to.
287
-
288
- ## Testing and Reset
289
-
290
- Testing your application with different sets of arguments can by simplified with `reset`. Call `MyConfig.reset` before each test and all configuration will be reset to its default value. There's no need to explicitly unset all your options between each run.
291
-
292
- NOTE: if you have arrays of arrays, or other deep nesting, we suggest you use code blocks to set up your default values (`default(:option) { [ [ 1, 2 ], [ 3, 4 ] ] }`). Deep children will not always be reset to their default values.
293
-
294
- Enjoy!
295
-
296
- ## Contributing
297
-
298
- For information on contributing to this project see <https://github.com/chef/chef/blob/master/CONTRIBUTING.md>
299
-
300
- ## License
301
-
302
- - Copyright:: Copyright (c) 2009-2016 Chef Software, Inc.
303
- - License:: Apache License, Version 2.0
304
-
305
- ```text
306
- Licensed under the Apache License, Version 2.0 (the "License");
307
- you may not use this file except in compliance with the License.
308
- You may obtain a copy of the License at
309
-
310
- http://www.apache.org/licenses/LICENSE-2.0
311
-
312
- Unless required by applicable law or agreed to in writing, software
313
- distributed under the License is distributed on an "AS IS" BASIS,
314
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
315
- See the License for the specific language governing permissions and
316
- limitations under the License.