diff options
author | John Keiser <john@johnkeiser.com> | 2015-06-03 15:55:26 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-06-23 15:23:01 -0700 |
commit | c7f314f08d8baa0d5d8743153e244f0b4daec5e3 (patch) | |
tree | b31b2e4bcd4face7082cd6f391f451c2dc83d02b | |
parent | 0a55c383bcd1f658f68c4ee1dbb2dd910cea052b (diff) | |
download | chef-c7f314f08d8baa0d5d8743153e244f0b4daec5e3.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 39435d557d..cbab5e9c27 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -774,6 +774,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) @@ -781,8 +783,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 |