diff options
author | rishichawda <rishichawda@users.noreply.github.com> | 2021-09-15 14:58:52 +0530 |
---|---|---|
committer | rishichawda <rishichawda@users.noreply.github.com> | 2021-09-15 15:26:21 +0530 |
commit | 574ec61f65ef58bf33fccfef93921aafa1b76252 (patch) | |
tree | 41083917bb6cc90bbe96f76aedb2fbf03d7a2596 | |
parent | 3c5c6db5147db002d3f1b8e00912e959fa0e6752 (diff) | |
download | chef-574ec61f65ef58bf33fccfef93921aafa1b76252.tar.gz |
lazy initialize unscrubbed values with a getter
Signed-off-by: rishichawda <rishichawda@users.noreply.github.com>
-rw-r--r-- | lib/chef/resource/registry_key.rb | 64 |
1 files changed, 32 insertions, 32 deletions
diff --git a/lib/chef/resource/registry_key.rb b/lib/chef/resource/registry_key.rb index 5119b9848a..a0f7c08780 100644 --- a/lib/chef/resource/registry_key.rb +++ b/lib/chef/resource/registry_key.rb @@ -121,6 +121,35 @@ class Chef default_action :create allowed_actions :create, :create_if_missing, :delete, :delete_key + VALID_VALUE_HASH_KEYS = %i{name type data}.freeze + + property :key, String, name_property: true + property :values, [Hash, Array], + default: [], + coerce: proc { |v| + case v + when Hash + @unscrubbed_values = [ Mash.new(v).symbolize_keys ] + when Array + @unscrubbed_values = v.map { |value| Mash.new(value).symbolize_keys } + else + @unscrubbed_values = [] + end + scrub_values(@unscrubbed_values) + }, + callbacks: { + "Missing name key in RegistryKey values hash" => lambda { |v| v.all? { |value| value.key?(:name) } }, + "Bad key in RegistryKey values hash. Should be one of: #{VALID_VALUE_HASH_KEYS}" => lambda do |v| + v.all? do |value| + value.keys.all? { |key| VALID_VALUE_HASH_KEYS.include?(key) } + end + end, + "Type of name should be a string" => lambda { |v| v.all? { |value| value[:name].is_a?(String) } }, + "Type of type should be a symbol" => lambda { |v| v.all? { |value| value[:type] ? value[:type].is_a?(Symbol) : true } }, + } + property :recursive, [TrueClass, FalseClass], default: false + property :architecture, Symbol, default: :machine, equal_to: %i{machine x86_64 i386} + # Some registry key data types may not be safely reported as json. # Example (CHEF-5323): # @@ -150,38 +179,9 @@ class Chef # may want to extend the state_attrs API with the ability to rename POST'd attrs. # # See lib/chef/resource_reporter.rb for more information. - attr_reader :unscrubbed_values - - VALID_VALUE_HASH_KEYS = %i{name type data}.freeze - - property :key, String, name_property: true - - property :values, [Hash, Array], - default: [], - coerce: proc { |v| - case v - when Hash - @unscrubbed_values = [ Mash.new(v).symbolize_keys ] - when Array - @unscrubbed_values = v.map { |value| Mash.new(value).symbolize_keys } - else - raise ArgumentError, "Bad type for RegistryKey resource, use Hash or Array" - end - scrub_values(@unscrubbed_values) - }, - callbacks: { - "Missing name key in RegistryKey values hash" => lambda { |v| v.all? { |value| value.key?(:name) } }, - "Bad key in RegistryKey values hash. Should be one of: #{VALID_VALUE_HASH_KEYS}" => lambda do |v| - v.all? do |value| - value.keys.all? { |key| VALID_VALUE_HASH_KEYS.include?(key) } - end - end, - "Type of name should be a string" => lambda { |v| v.all? { |value| value[:name].is_a?(String) } }, - "Type of type should be a symbol" => lambda { |v| v.all? { |value| value[:type] ? value[:type].is_a?(Symbol) : true } }, - } - - property :recursive, [TrueClass, FalseClass], default: false - property :architecture, Symbol, default: :machine, equal_to: %i{machine x86_64 i386} + def unscrubbed_values + @unscrubbed_values ||= [] + end private |