summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Vargo <sethvargo@gmail.com>2014-06-26 12:32:11 -0700
committerSeth Vargo <sethvargo@gmail.com>2014-07-11 14:55:59 -0400
commit9a474f1c670fd68b757aa9d62378875c6c1f7abc (patch)
tree6da2fc812eee846e2bd6a5c57a359b40fb53f40f
parentfc8f9ef17261a05ba8cd3b882711c91676988123 (diff)
downloadchef-9a474f1c670fd68b757aa9d62378875c6c1f7abc.tar.gz
Allow default values to be a DelayedEvaluator
-rw-r--r--lib/chef/mixin/params_validate.rb7
-rw-r--r--lib/chef/resource/lwrp_base.rb4
-rw-r--r--spec/unit/lwrp_spec.rb46
3 files changed, 57 insertions, 0 deletions
diff --git a/lib/chef/mixin/params_validate.rb b/lib/chef/mixin/params_validate.rb
index a9799f749c..bedc67f357 100644
--- a/lib/chef/mixin/params_validate.rb
+++ b/lib/chef/mixin/params_validate.rb
@@ -95,6 +95,13 @@ class Chef
val = arg
else
val = validate({ symbol => arg }, { 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 val.is_a?(DelayedEvaluator)
+ val = val.call(self)
+ end
end
self.instance_variable_set(iv_symbol, val)
end
diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb
index e91516e6f6..7b4e8bf4f1 100644
--- a/lib/chef/resource/lwrp_base.rb
+++ b/lib/chef/resource/lwrp_base.rb
@@ -119,6 +119,10 @@ class Chef
run_context.node
end
+ def self.lazy(&block)
+ DelayedEvaluator.new(&block)
+ end
+
# Default initializer. Sets the default action and allowed actions.
def initialize(name, run_context=nil)
super(name, run_context)
diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb
index 0e0ea5dbb7..5c17173249 100644
--- a/spec/unit/lwrp_spec.rb
+++ b/spec/unit/lwrp_spec.rb
@@ -133,6 +133,52 @@ describe "LWRP" do
cls.node[:penguin_name].should eql("jackass")
end
+ context "resource_name" do
+ let(:klass) { Class.new(Chef::Resource::LWRPBase) }
+
+ it "returns nil when the resource_name is not set" do
+ expect(klass.resource_name).to be_nil
+ end
+
+ it "allows to user to user the resource_name" do
+ expect {
+ klass.resource_name(:foo)
+ }.to_not raise_error
+ end
+
+ it "returns the set value for the resource" do
+ klass.resource_name(:foo)
+ expect(klass.resource_name).to eq(:foo)
+ end
+
+ context "when creating a new instance" do
+ it "raises an exception if resource_name is nil" do
+ expect {
+ klass.new('blah')
+ }.to raise_error(Chef::Exceptions::InvalidResourceSpecification)
+ end
+ end
+
+ context "lazy default values" do
+ let(:klass) do
+ Class.new(Chef::Resource::LWRPBase) do
+ self.resource_name = :sample_resource
+ attribute :food, default: lazy { 'BACON!'*3 }
+ attribute :drink, default: lazy { |r| "Drink after #{r.food}!"}
+ end
+ end
+
+ let(:instance) { klass.new('kitchen') }
+
+ it "evaluates the default value when requested" do
+ expect(instance.food).to eq('BACON!BACON!BACON!')
+ end
+
+ it "evaluates yields self to the block" do
+ expect(instance.drink).to eq('Drink after BACON!BACON!BACON!!')
+ end
+ end
+ end
end
describe "Lightweight Chef::Provider" do