diff options
author | John Keiser <john@johnkeiser.com> | 2015-06-04 10:11:25 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-06-23 15:23:02 -0700 |
commit | 6e4783f4e71681c7a6fefedde3e71fd60b1488a3 (patch) | |
tree | 22b8ce7fa912d9e9d3763af56351efe99e0bed2a /lib/chef | |
parent | 1d96b8c7bb96a476625a026bea58dd0f251af716 (diff) | |
download | chef-6e4783f4e71681c7a6fefedde3e71fd60b1488a3.tar.gz |
Don't validate `nil` when setting the value to nil
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/mixin/params_validate.rb | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/lib/chef/mixin/params_validate.rb b/lib/chef/mixin/params_validate.rb index 2d382d8381..9cd70eede7 100644 --- a/lib/chef/mixin/params_validate.rb +++ b/lib/chef/mixin/params_validate.rb @@ -87,34 +87,39 @@ class Chef iv_symbol = "@#{symbol.to_s}".to_sym # If the user passed NOT_PASSED, or passed nil, then this is a get. - case value - when NOT_PASSED - is_get = true - value = nil - when nil - is_get = true unless explicitly_allows_nil?(symbol, validation) - end + if value == NOT_PASSED || (value.nil? && !explicitly_allows_nil?(symbol, validation)) - if self.instance_variable_defined?(iv_symbol) && is_get - value = self.instance_variable_get(iv_symbol) - if value.is_a?(DelayedEvaluator) - validate({ symbol => value.call }, { symbol => validation })[symbol] - else - value - end - else - if !value.is_a?(DelayedEvaluator) - value = validate({ symbol => value }, { symbol => validation })[symbol] + # Get the value if there is one + if self.instance_variable_defined?(iv_symbol) + value = self.instance_variable_get(iv_symbol) + if value.is_a?(DelayedEvaluator) + value = validate({ symbol => value.call }, { symbol => validation })[symbol] + end + # Get the default value + else + value = validate({}, { symbol => validation })[symbol] # Handle the case where the "default" was a DelayedEvaluator. In # this case, the block yields an optional parameter of +self+, # which is the equivalent of "new_resource" if value.is_a?(DelayedEvaluator) value = value.call(self) end + + # Defaults are presently "stickily" set on the instance + self.instance_variable_set(iv_symbol, value) end + + # Set the value + else + unless value.is_a?(DelayedEvaluator) + value = validate({ symbol => value }, { symbol => validation })[symbol] + end + self.instance_variable_set(iv_symbol, value) end + + value end private |