summaryrefslogtreecommitdiff
path: root/lib/chef
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-04 10:43:38 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-23 15:23:02 -0700
commitf47252a75191d96e169abdc7c9d84aa6e50df708 (patch)
tree45dcb92ca96ff5b1881bc227236e81a86a3e946a /lib/chef
parent2d7f1282c63ca6480f363603608e9d3cdf38116e (diff)
downloadchef-f47252a75191d96e169abdc7c9d84aa6e50df708.tar.gz
Add property_is_set?
Diffstat (limited to 'lib/chef')
-rw-r--r--lib/chef/mixin/params_validate.rb27
-rw-r--r--lib/chef/resource.rb9
2 files changed, 26 insertions, 10 deletions
diff --git a/lib/chef/mixin/params_validate.rb b/lib/chef/mixin/params_validate.rb
index 142c582cdd..ebab964b74 100644
--- a/lib/chef/mixin/params_validate.rb
+++ b/lib/chef/mixin/params_validate.rb
@@ -84,7 +84,8 @@ class Chef
end
def set_or_return(symbol, value, validation)
- iv_symbol = "@#{symbol.to_s}".to_sym
+ symbol = symbol.to_sym
+ iv_symbol = :"@#{symbol}"
# If the user passed NOT_PASSED, or passed nil, then this is a get.
if value == NOT_PASSED || (value.nil? && !explicitly_allows_nil?(symbol, validation))
@@ -98,16 +99,22 @@ class Chef
# 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
+ validated = validate({}, { symbol => validation })
+ if validated.has_key?(symbol)
+ value = validated[symbol]
- # Defaults are presently "stickily" set on the instance
- self.instance_variable_set(iv_symbol, value)
+ # 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)
+ else
+ value = nil
+ end
end
# Set the value
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 9496656705..8d2532dac4 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -791,6 +791,15 @@ class Chef
end
#
+ # Whether this property has been set (or whether it has a default that has
+ # been retrieved).
+ #
+ def property_is_set?(name)
+ name = name.to_sym
+ instance_variable_defined?("@#{name}")
+ end
+
+ #
# Create a lazy value for assignment to a default value.
#
# @param block The block to run when the value is retrieved.