mixlib-config 2.2.11 → 3.0.5

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: c764853ffe7576f6b9568775541cc9358cc6a392a26997af52ae0c9dcde1611e
4
- data.tar.gz: 19713a0e41d1c407510483d11df8f6ac83e8645d5607890b5b7181fcf0e51689
3
+ metadata.gz: 542f1c9f7daff1297b70799397e6f44dba7896522e89334e71496db690802013
4
+ data.tar.gz: f2383ad0c71049dab95ceeac81d39d7b8ed1d8cb2e9a6ed45ac29acaa0d63838
5
5
  SHA512:
6
- metadata.gz: 99c7c6639a2f2a1de69db7970d99ea5ca2a54fba04ac544967aba9a1552f3541a856b4d447098ee73364ac1094da33e7d5bdda92a87296e19d1d2dbbc5656ae2
7
- data.tar.gz: 95be61fa365942dc7a0a7a1d1bcc5b0eff1e9a362ed9207a138e1dc362a7051c8a546d6b1bc1c2a40113bf93d83571a2fde96473ae1fe576e013faa0bdc3c78e
6
+ metadata.gz: d4137a9a0747261ad42cc1f3035ab6136823ee3c89666e7adf041e36190cd24179e0eee5711686011ab7c1e7efead2242aef88c6f9c0e03bfee8ef41509492be
7
+ data.tar.gz: 22248d0a227c55598fa81debb29a0eaa9436d27192bfbd45beb091b0ace555b4abd4783f09ef7c1febca965b386320268dada9fe5f45a94485cc266695144a9b
data/lib/mixlib/config.rb CHANGED
@@ -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,11 +141,20 @@ 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
 
149
+ def is_default?(key)
150
+ symbol = key.to_sym
151
+ if configurables.key?(symbol)
152
+ configurables[symbol].is_default?(configuration)
153
+ else
154
+ raise ArgumentError, "config option must exist, and not be a context to check for default values"
155
+ end
156
+ end
157
+
161
158
  # Resets a config option to its default.
162
159
  #
163
160
  # === Parameters
@@ -168,8 +165,8 @@ module Mixlib
168
165
 
169
166
  # Resets all config options to their defaults.
170
167
  def reset
171
- self.configuration = Hash.new
172
- config_contexts.values.each { |config_context| config_context.reset }
168
+ self.configuration = ({})
169
+ config_contexts.values.each(&:reset)
173
170
  end
174
171
 
175
172
  # Makes a copy of any non-default values.
@@ -253,9 +250,9 @@ module Mixlib
253
250
  # === Returns
254
251
  # self
255
252
  def restore(hash)
256
- self.configuration = hash.reject { |key, value| config_contexts.has_key?(key) }
253
+ self.configuration = hash.reject { |key, value| config_contexts.key?(key) }
257
254
  config_contexts.each do |key, config_context|
258
- if hash.has_key?(key)
255
+ if hash.key?(key)
259
256
  config_context.restore(hash[key])
260
257
  else
261
258
  config_context.reset
@@ -263,7 +260,7 @@ module Mixlib
263
260
  end
264
261
  config_context_lists.each do |key, meta|
265
262
  meta[:values] = []
266
- if hash.has_key?(key)
263
+ if hash.key?(key)
267
264
  hash[key].each do |val|
268
265
  context = define_context(meta[:definition_blocks])
269
266
  context.restore(val)
@@ -273,7 +270,7 @@ module Mixlib
273
270
  end
274
271
  config_context_hashes.each do |key, meta|
275
272
  meta[:values] = {}
276
- if hash.has_key?(key)
273
+ if hash.key?(key)
277
274
  hash[key].each do |vkey, val|
278
275
  context = define_context(meta[:definition_blocks])
279
276
  context.restore(val)
@@ -292,7 +289,7 @@ module Mixlib
292
289
  # self
293
290
  def merge!(hash)
294
291
  hash.each do |key, value|
295
- if config_contexts.has_key?(key)
292
+ if config_contexts.key?(key)
296
293
  # Grab the config context and let internal_get cache it if so desired
297
294
  config_contexts[key].restore(value)
298
295
  else
@@ -372,9 +369,10 @@ module Mixlib
372
369
  # The value of the config option.
373
370
  def configurable(symbol, &block)
374
371
  unless configurables[symbol]
375
- if config_contexts.has_key?(symbol)
372
+ if config_contexts.key?(symbol)
376
373
  raise ReopenedConfigContextWithConfigurableError, "Cannot redefine config_context #{symbol} as a configurable value"
377
374
  end
375
+
378
376
  configurables[symbol] = Configurable.new(symbol)
379
377
  define_attr_accessor_methods(symbol)
380
378
  end
@@ -400,11 +398,11 @@ module Mixlib
400
398
  # block<Block>: a block that will be run in the context of this new config
401
399
  # class.
402
400
  def config_context(symbol, &block)
403
- if configurables.has_key?(symbol)
401
+ if configurables.key?(symbol)
404
402
  raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{symbol} with a config context"
405
403
  end
406
404
 
407
- if config_contexts.has_key?(symbol)
405
+ if config_contexts.key?(symbol)
408
406
  context = config_contexts[symbol]
409
407
  else
410
408
  context = Class.new
@@ -438,11 +436,11 @@ module Mixlib
438
436
  # block<Block>: a block that will be run in the context of this new config
439
437
  # class.
440
438
  def config_context_list(plural_symbol, singular_symbol, &block)
441
- if configurables.has_key?(plural_symbol)
439
+ if configurables.key?(plural_symbol)
442
440
  raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{plural_symbol} with a config context"
443
441
  end
444
442
 
445
- unless config_context_lists.has_key?(plural_symbol)
443
+ unless config_context_lists.key?(plural_symbol)
446
444
  config_context_lists[plural_symbol] = {
447
445
  definition_blocks: [],
448
446
  values: [],
@@ -470,11 +468,11 @@ module Mixlib
470
468
  # block<Block>: a block that will be run in the context of this new config
471
469
  # class.
472
470
  def config_context_hash(plural_symbol, singular_symbol, &block)
473
- if configurables.has_key?(plural_symbol)
471
+ if configurables.key?(plural_symbol)
474
472
  raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{plural_symbol} with a config context"
475
473
  end
476
474
 
477
- unless config_context_hashes.has_key?(plural_symbol)
475
+ unless config_context_hashes.key?(plural_symbol)
478
476
  config_context_hashes[plural_symbol] = {
479
477
  definition_blocks: [],
480
478
  values: {},
@@ -534,9 +532,10 @@ module Mixlib
534
532
  # <ArgumentError>:: if value is set to something other than true, false, or :warn
535
533
  #
536
534
  def config_strict_mode=(value)
537
- if ![ true, false, :warn, nil ].include?(value)
535
+ unless [ true, false, :warn, nil ].include?(value)
538
536
  raise ArgumentError, "config_strict_mode must be true, false, nil or :warn"
539
537
  end
538
+
540
539
  @config_strict_mode = value
541
540
  end
542
541
 
@@ -558,6 +557,28 @@ module Mixlib
558
557
  internal_get_or_set(method_symbol, *args)
559
558
  end
560
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
+
561
582
  private
562
583
 
563
584
  # Given a (nested) Hash, turn it into a single top-level hash using dots as
@@ -588,9 +609,9 @@ module Mixlib
588
609
  # value<Object>:: Value to be set in config hash
589
610
  #
590
611
  def internal_set(symbol, value)
591
- if configurables.has_key?(symbol)
612
+ if configurables.key?(symbol)
592
613
  configurables[symbol].set(configuration, value)
593
- elsif config_contexts.has_key?(symbol)
614
+ elsif config_contexts.key?(symbol)
594
615
  config_contexts[symbol].restore(value.to_hash)
595
616
  else
596
617
  if config_strict_mode == :warn
@@ -603,13 +624,13 @@ module Mixlib
603
624
  end
604
625
 
605
626
  def internal_get(symbol)
606
- if configurables.has_key?(symbol)
627
+ if configurables.key?(symbol)
607
628
  configurables[symbol].get(configuration)
608
- elsif config_contexts.has_key?(symbol)
629
+ elsif config_contexts.key?(symbol)
609
630
  config_contexts[symbol]
610
- elsif config_context_lists.has_key?(symbol)
631
+ elsif config_context_lists.key?(symbol)
611
632
  config_context_lists[symbol]
612
- elsif config_context_hashes.has_key?(symbol)
633
+ elsif config_context_hashes.key?(symbol)
613
634
  config_context_hashes[symbol]
614
635
  else
615
636
  if config_strict_mode == :warn
@@ -686,7 +707,7 @@ module Mixlib
686
707
  # Adds a single new context to the list
687
708
  meta.send :define_method, singular_symbol do |key, &block|
688
709
  context_hash_details = internal_get(plural_symbol)
689
- context = if context_hash_details[:values].has_key? key
710
+ context = if context_hash_details[:values].key? key
690
711
  context_hash_details[:values][key]
691
712
  else
692
713
  new_context = define_context(context_hash_details[:definition_blocks])
@@ -1,6 +1,6 @@
1
1
  #
2
2
  # Author:: John Keiser (<jkeiser@chef.io>)
3
- # Copyright:: Copyright (c) 2013-2016 Chef Software, Inc.
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
- attr_reader :has_default
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.has_key?(@symbol)
46
- config[@symbol]
47
- elsif @default_block
48
- @default_block.call
56
+ if config.key?(symbol)
57
+ config[symbol]
58
+ elsif default_block?
59
+ default_block.call
49
60
  else
50
- begin
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[@symbol] = @writes_value ? @writes_value.call(value) : value
66
+ config[symbol] = writes_value? ? @writes_value.call(value) : value
62
67
  end
63
68
 
64
69
  def default
65
- if @default_block
66
- @default_block.call
70
+ if default_block?
71
+ default_block.call
67
72
  else
68
- @default_value
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
@@ -19,7 +19,7 @@
19
19
  module Mixlib
20
20
  module Config
21
21
 
22
- VERSION = "2.2.11"
22
+ VERSION = "3.0.5".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.11
4
+ version: 3.0.5
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-02 00:00:00.000000000 Z
11
+ date: 2019-11-14 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