diff options
author | John Keiser <john@johnkeiser.com> | 2015-06-03 15:55:26 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-06-18 15:26:08 -0700 |
commit | b8f40d910ce751581b28f8826857afa62b0419a0 (patch) | |
tree | 032aa4dc393d645a9eec7b86375912cf511d2b0e | |
parent | 70ffc2572016e4a7f23d7b7805b9e0c1cde6a6b1 (diff) | |
download | chef-b8f40d910ce751581b28f8826857afa62b0419a0.tar.gz |
Add property= setter to properties
-rw-r--r-- | lib/chef/resource.rb | 10 | ||||
-rw-r--r-- | spec/unit/property_spec.rb | 38 |
2 files changed, 27 insertions, 21 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 8c4e140108..c3167fc88d 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -770,6 +770,8 @@ class Chef # property :x, String, default: 'hi' # def self.property(name, type=NULL_ARG, **options) + name = name.to_sym + if type != NULL_ARG if options[:is] options[:is] = ([ type ] + [ options[:is] ]).flatten(1) @@ -777,8 +779,12 @@ class Chef options[:is] = type end end - define_method(name) do |arg=nil| - set_or_return(name.to_sym, arg, options) + + define_method(name) do |value=nil| + set_or_return(name, value, options) + end + define_method("#{name}=") do |value| + set_or_return(name, value, options) end end diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb index 5f88561f3a..908f7f549d 100644 --- a/spec/unit/property_spec.rb +++ b/spec/unit/property_spec.rb @@ -87,20 +87,20 @@ describe "Chef::Resource.property" do expect(resource.bare_property 20).to eq 20 expect(resource.bare_property).to eq 20 end - # it "can be set with =" do - # expect(resource.bare_property 10).to eq 10 - # expect(resource.bare_property).to eq 10 - # end + it "can be set with =" do + expect(resource.bare_property 10).to eq 10 + expect(resource.bare_property).to eq 10 + end # it "can be set to nil with =" do # expect(resource.bare_property 10).to eq 10 # expect(resource.bare_property = nil).to be_nil # expect(resource.bare_property).to be_nil # end - # it "can be updated with =" do - # expect(resource.bare_property 10).to eq 10 - # expect(resource.bare_property = 20).to eq 20 - # expect(resource.bare_property).to eq 20 - # end + it "can be updated with =" do + expect(resource.bare_property 10).to eq 10 + expect(resource.bare_property = 20).to eq 20 + expect(resource.bare_property).to eq 20 + end end with_property ":x, Integer" do @@ -118,8 +118,8 @@ describe "Chef::Resource.property" do it "x is inherited" do expect(subresource.x 10).to eq 10 expect(subresource.x).to eq 10 - # expect(subresource.x = 20).to eq 20 - # expect(subresource.x).to eq 20 + expect(subresource.x = 20).to eq 20 + expect(subresource.x).to eq 20 # expect(subresource_class.properties[:x]).not_to be_nil end @@ -137,15 +137,15 @@ describe "Chef::Resource.property" do it "x is still there" do expect(subresource.x 10).to eq 10 expect(subresource.x).to eq 10 - # expect(subresource.x = 20).to eq 20 - # expect(subresource.x).to eq 20 + expect(subresource.x = 20).to eq 20 + expect(subresource.x).to eq 20 # expect(subresource_class.properties[:x]).not_to be_nil end it "y is there" do expect(subresource.y 10).to eq 10 expect(subresource.y).to eq 10 - # expect(subresource.y = 20).to eq 20 - # expect(subresource.y).to eq 20 + expect(subresource.y = 20).to eq 20 + expect(subresource.y).to eq 20 # expect(subresource_class.properties[:y]).not_to be_nil end it "y is not on the superclass" do @@ -164,8 +164,8 @@ describe "Chef::Resource.property" do it "x is still there" do expect(subresource.x 10).to eq 10 expect(subresource.x).to eq 10 - # expect(subresource.x = 20).to eq 20 - # expect(subresource.x).to eq 20 + expect(subresource.x = 20).to eq 20 + expect(subresource.x).to eq 20 # expect(subresource_class.properties[:x]).not_to be_nil # expect(subresource_class.properties[:x]).not_to eq resource_class.properties[:x] end @@ -190,8 +190,8 @@ describe "Chef::Resource.property" do it "x is still there" do expect(subresource.x "10").to eq "10" expect(subresource.x).to eq "10" - # expect(subresource.x = "20").to eq "20" - # expect(subresource.x).to eq "20" + expect(subresource.x = "20").to eq "20" + expect(subresource.x).to eq "20" # expect(subresource_class.properties[:x]).not_to be_nil # expect(subresource_class.properties[:x]).not_to eq resource_class.properties[:x] end |