diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2018-07-05 16:47:30 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2018-07-05 16:47:30 -0700 |
commit | 59afc58673637e93a54a318ddf4a851133554af6 (patch) | |
tree | 6f03097230af300da0a6f2a5e404ec416ae453f9 | |
parent | 7c9fa3f22ec91789aac9ffcf73f4fb2c9d4e790d (diff) | |
download | mixlib-config-59afc58673637e93a54a318ddf4a851133554af6.tar.gz |
add is_default? inspection method
plus some code cleanup of the configurable class
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | README.md | 12 | ||||
-rw-r--r-- | lib/mixlib/config.rb | 11 | ||||
-rw-r--r-- | lib/mixlib/config/configurable.rb | 63 | ||||
-rw-r--r-- | spec/mixlib/config_spec.rb | 17 |
4 files changed, 80 insertions, 23 deletions
@@ -248,6 +248,18 @@ This allows the user to quickly specify a number of values with one default, whi print_network_requests false ``` +You can also inspect if the values are still their defaults or not: + +```ruby +MyConfig.is_default?(:verbosity) # == true +MyConfig[:verbosity] = 5 +MyConfig.is_default?(:verbosity) # == false +MyConfig[:verbosity] = 1 +MyConfig.is_default?(:verbosity) # == true +``` + +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. + ## Strict Mode 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`. diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb index 6a935ae..adb8bbf 100644 --- a/lib/mixlib/config.rb +++ b/lib/mixlib/config.rb @@ -158,6 +158,17 @@ module Mixlib alias_method :has_key?, :key? + def is_default?(key) + pp configurables + pp configuration + symbol = key.to_sym + if configurables.has_key?(symbol) + configurables[symbol].is_default?(configuration) + else + raise ArgumentError, "config option must exist, and not be a context to check for default values" + end + end + # Resets a config option to its default. # # === Parameters diff --git a/lib/mixlib/config/configurable.rb b/lib/mixlib/config/configurable.rb index e1b5b14..df2ffd9 100644 --- a/lib/mixlib/config/configurable.rb +++ b/lib/mixlib/config/configurable.rb @@ -1,6 +1,6 @@ # # Author:: John Keiser (<jkeiser@chef.io>) -# Copyright:: Copyright (c) 2013-2016 Chef Software, Inc. +# Copyright:: Copyright (c) 2013-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,20 +19,31 @@ module Mixlib module Config class Configurable + attr_reader :symbol + attr_reader :default_value + attr_reader :default_block + def initialize(symbol) @symbol = symbol - @default_block = nil - @has_default = false - @default_value = nil - @writes_value = nil end - attr_reader :has_default + def has_default? + instance_variable_defined?(:@default_value) + end + + def writes_value? + instance_variable_defined?(:@writes_value) + end + + def default_block? + instance_variable_defined?(:@default_block) + end + + alias_method :has_default, :has_default? def defaults_to(default_value = nil, &block) - @has_default = true - @default_block = block @default_value = default_value + @default_block = block if block_given? self end @@ -42,32 +53,38 @@ module Mixlib end def get(config) - if config.has_key?(@symbol) - config[@symbol] - elsif @default_block - @default_block.call + if config.key?(symbol) + config[symbol] + elsif default_block? + default_block.call else - begin - # Some things cannot be dup'd, and you won't know this till after the fact - # because all values implement dup - config[@symbol] = @default_value.dup - rescue TypeError - @default_value - end + config[symbol] = safe_dup(default_value) end end def set(config, value) - config[@symbol] = @writes_value ? @writes_value.call(value) : value + config[symbol] = writes_value? ? @writes_value.call(value) : value end def default - if @default_block - @default_block.call + if default_block? + default_block.call else - @default_value + default_value end end + + def is_default?(config) + !config.key?(symbol) || config[symbol] == default_value + end + + private + + def safe_dup(e) + e.dup + rescue TypeError + e + end end end end diff --git a/spec/mixlib/config_spec.rb b/spec/mixlib/config_spec.rb index c664ceb..1786789 100644 --- a/spec/mixlib/config_spec.rb +++ b/spec/mixlib/config_spec.rb @@ -235,6 +235,23 @@ describe Mixlib::Config do expect(@klass.respond_to?("z=".to_sym)).to be false end + it "returns true for is_default? for a default value" do + expect(@klass[:a]).to eql(1) + expect(@klass.is_default?(:a)).to be true + end + + it "returns true for is_default? for an overwritten default value" do + @klass[:a] = 1 + expect(@klass[:a]).to eql(1) + expect(@klass.is_default?(:a)).to be true + end + + it "returns false for is_default? for a value that is not the default" do + @klass[:a] = 2 + expect(@klass[:a]).to eql(2) + expect(@klass.is_default?(:a)).to be false + end + describe "and extra methods have been dumped into Object" do class NopeError < StandardError end |