From 6e4783f4e71681c7a6fefedde3e71fd60b1488a3 Mon Sep 17 00:00:00 2001 From: John Keiser Date: Thu, 4 Jun 2015 10:11:25 -0700 Subject: Don't validate `nil` when setting the value to nil --- lib/chef/mixin/params_validate.rb | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'lib/chef') 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 -- cgit v1.2.1