diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2018-09-26 22:22:29 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2018-09-26 22:22:29 -0700 |
commit | 8400f4cb7d977896531ae277fdcf3398dc2d4c6c (patch) | |
tree | 33b8b5ba385366457de4990636daa143d64f1043 /lib/chef/cookbook/manifest_v0.rb | |
parent | 8daa1c6598182777802eba6a74928f28a32f6835 (diff) | |
download | chef-8400f4cb7d977896531ae277fdcf3398dc2d4c6c.tar.gz |
replace some instances of to_hash with to_hlcg/to-h-cleanup
to_hash on a lot of these objects should go away, but even eliminating
all our calls to to_hash on these objects internally is difficult.
(e.g. converting the knife ui code to call #to_h means we wind up
calling nil#to_h which "helpfully" becomes '{}' which is hilarious and
i don't know why someone thought that was a good idea).
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'lib/chef/cookbook/manifest_v0.rb')
-rw-r--r-- | lib/chef/cookbook/manifest_v0.rb | 63 |
1 files changed, 34 insertions, 29 deletions
diff --git a/lib/chef/cookbook/manifest_v0.rb b/lib/chef/cookbook/manifest_v0.rb index 5a6d7401fa..198c7a4741 100644 --- a/lib/chef/cookbook/manifest_v0.rb +++ b/lib/chef/cookbook/manifest_v0.rb @@ -1,5 +1,5 @@ # Author:: Daniel DeLeo (<dan@chef.io>) -# Copyright:: Copyright 2015-2016, Chef Software Inc. +# Copyright:: Copyright 2015-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,42 +26,47 @@ class Chef COOKBOOK_SEGMENTS = %w{ resources providers recipes definitions libraries attributes files templates root_files }.freeze - def self.from_hash(hash) - response = Mash.new(hash) - response[:all_files] = COOKBOOK_SEGMENTS.inject([]) do |memo, segment| - next memo if hash[segment].nil? || hash[segment].empty? - hash[segment].each do |file| - file["name"] = "#{segment}/#{file["name"]}" - memo << file + class << self + + def from_hash(hash) + response = Mash.new(hash) + response[:all_files] = COOKBOOK_SEGMENTS.inject([]) do |memo, segment| + next memo if hash[segment].nil? || hash[segment].empty? + hash[segment].each do |file| + file["name"] = "#{segment}/#{file["name"]}" + memo << file + end + response.delete(segment) + memo end - response.delete(segment) - memo + response end - response - end - def self.to_hash(manifest) - result = manifest.manifest.dup - result.delete("all_files") + def to_h(manifest) + result = manifest.manifest.dup + result.delete("all_files") - files = manifest.by_parent_directory - files.keys.each_with_object(result) do |parent, memo| - if COOKBOOK_SEGMENTS.include?(parent) - memo[parent] ||= [] - files[parent].each do |file| - file["name"] = file["name"].split("/")[1] - file.delete("full_path") - memo[parent] << file + files = manifest.by_parent_directory + files.keys.each_with_object(result) do |parent, memo| + if COOKBOOK_SEGMENTS.include?(parent) + memo[parent] ||= [] + files[parent].each do |file| + file["name"] = file["name"].split("/")[1] + file.delete("full_path") + memo[parent] << file + end end end - end - # Ensure all segments are set to [] if they don't exist. - # See https://github.com/chef/chef/issues/6044 - COOKBOOK_SEGMENTS.each do |segment| - result[segment] ||= [] + # Ensure all segments are set to [] if they don't exist. + # See https://github.com/chef/chef/issues/6044 + COOKBOOK_SEGMENTS.each do |segment| + result[segment] ||= [] + end + + result.merge({ "frozen?" => manifest.frozen_version?, "chef_type" => "cookbook_version" }) end - result.merge({ "frozen?" => manifest.frozen_version?, "chef_type" => "cookbook_version" }) + alias_method :to_hash, :to_h end end end |