summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-03 16:01:07 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-23 15:23:02 -0700
commita2ddd1133c57f7ae25cdebd0eb860bb8c3b148ad (patch)
tree9a0eeb99a23748c71775317bb52eebb9025de9a5
parentc7f314f08d8baa0d5d8743153e244f0b4daec5e3 (diff)
downloadchef-a2ddd1133c57f7ae25cdebd0eb860bb8c3b148ad.tar.gz
Add lazy to Resource class DSL
-rw-r--r--lib/chef/resource.rb11
-rw-r--r--spec/unit/property_spec.rb12
2 files changed, 15 insertions, 8 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index cbab5e9c27..44247ca9f0 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -792,6 +792,17 @@ class Chef
end
end
+ #
+ # Create a lazy value for assignment to a default value.
+ #
+ # @param block The block to run when the value is retrieved.
+ #
+ # @return [Chef::DelayedEvaluator] The lazy value
+ #
+ def self.lazy(&block)
+ DelayedEvaluator.new(&block)
+ end
+
# Set or return the list of "state attributes" implemented by the Resource
# subclass. State attributes are attributes that describe the desired state
# of the system, such as file permissions or ownership. In general, state
diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb
index 908f7f549d..d9fc4fa41f 100644
--- a/spec/unit/property_spec.rb
+++ b/spec/unit/property_spec.rb
@@ -368,8 +368,7 @@ describe "Chef::Resource.property" do
end
end
- # with_property ':x, default: lazy { {} }' do
- with_property ':x, default: Chef::DelayedEvaluator.new { {} }' do
+ with_property ':x, default: lazy { {} }' do
it "when x is not set, it returns {}" do
expect(resource.x).to eq({})
end
@@ -396,8 +395,7 @@ describe "Chef::Resource.property" do
end
end
- # with_property ':x, default: lazy { blah }' do
- with_property ':x, default: Chef::DelayedEvaluator.new { blah }' do
+ with_property ':x, default: lazy { blah }' do
# it "x is run in context of the instance" do
# expect(resource.x).to eq "blah1"
# end
@@ -411,8 +409,7 @@ describe "Chef::Resource.property" do
end
end
- # with_property ':x, default: lazy { |x| "#{blah}#{x.blah}" }' do
- with_property ':x, default: Chef::DelayedEvaluator.new { |x| "#{blah}#{x.blah}" }' do
+ with_property ':x, default: lazy { |x| "#{blah}#{x.blah}" }' do
it "x is run in context of the class (where it was defined) and passed the instance" do
expect(resource.x).to eq "classblah1"
end
@@ -438,8 +435,7 @@ describe "Chef::Resource.property" do
end
end
- # with_property ":x, String, default: lazy { Namer.next_index }" do
- with_property ":x, String, default: Chef::DelayedEvaluator.new { Namer.next_index }" do
+ with_property ":x, String, default: lazy { Namer.next_index }" do
it "when the resource is created, no error is raised" do
resource
end