diff options
author | John Keiser <john@johnkeiser.com> | 2015-07-21 16:47:26 -0600 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-07-27 09:38:25 -0600 |
commit | 9b957381e30423c32a52e3b9bb6ba2a6b0a09b64 (patch) | |
tree | 05305cd9dc6a74e2ab5486fd8b9fe2a2b633d723 /lib | |
parent | 5e1567fdeb2b0693000fde7d702c106dc51b335c (diff) | |
download | chef-9b957381e30423c32a52e3b9bb6ba2a6b0a09b64.tar.gz |
Make "property_name" in actions load current value as default
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/provider.rb | 21 | ||||
-rw-r--r-- | lib/chef/resource.rb | 7 |
2 files changed, 25 insertions, 3 deletions
diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb index dcfc92645b..43d58740f2 100644 --- a/lib/chef/provider.rb +++ b/lib/chef/provider.rb @@ -211,10 +211,29 @@ class Chef extend Forwardable define_singleton_method(:to_s) { "#{resource_class} forwarder module" } define_singleton_method(:inspect) { to_s } + # Add a delegator for each explicit property that will get the *current* value + # of the property by default instead of the *actual* value. + resource.class.properties.each do |name, property| + class_eval(<<-EOM, __FILE__, __LINE__) + def #{name}(*args, &block) + # If no arguments were passed, we process "get" by defaulting + # the value to current_resource, not new_resource. This helps + # avoid issues where resources accidentally overwrite perfectly + # valid stuff with default values. + if args.empty? && !block + if !new_resource.property_is_set?(__method__) && current_resource + return current_resource.public_send(__method__) + end + end + new_resource.public_send(__method__, *args, &block) + end + EOM + end dsl_methods = resource.class.public_instance_methods + resource.class.protected_instance_methods - - provider_class.instance_methods + provider_class.instance_methods - + resource.class.properties.keys def_delegators(:new_resource, *dsl_methods) end include @included_resource_dsl_module diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index cdb351f643..bb5c6050fe 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -1442,9 +1442,12 @@ class Chef use_inline_resources include_resource_dsl true define_singleton_method(:to_s) { "#{resource_class} action provider" } - define_singleton_method(:inspect) { to_s } - define_method(:load_current_resource) do + def self.inspect + to_s + end + def load_current_resource if new_resource.respond_to?(:load_current_value!) + # dup the resource and then reset desired-state properties. current_resource = new_resource.dup # We clear desired state in the copy, because it is supposed to be actual state. |