From b671a8036b6c00461ecb97dbb51e50899bac7109 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Mon, 12 Feb 2018 17:02:18 -0800 Subject: add Chef::NodeMap#delete_class API halite needs this public API in order to not be so brittle. needs tests... Signed-off-by: Lamont Granquist --- lib/chef/node_map.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/chef/node_map.rb b/lib/chef/node_map.rb index dde93a437d..ed183a70ee 100644 --- a/lib/chef/node_map.rb +++ b/lib/chef/node_map.rb @@ -118,6 +118,29 @@ class Chef end.map { |matcher| matcher[:klass] } end + # Remove a class from all its matchers in the node_map, will remove mappings completely if its the last matcher left + # + # @param klass [Class] the class to seek and destroy + # + # @return [Hash] deleted entries in the same format as the @map + def delete_class(klass) + raise "please use a Class type for the klass argument" unless klass.is_a?(Class) + deleted = {} + map.each do |key, matchers| + deleted_matchers = [] + matchers.delete_if do |matcher| + # because matcher[:klass] may be a string (which needs to die), coerce both to strings to compare somewhat canonically + if matcher[:klass].to_s == klass.to_s + deleted_matchers << matcher + true + end + end + deleted[key] = deleted_matchers unless deleted_matchers.empty? + map.delete(key) if matchers.empty? + end + deleted + end + # Seriously, don't use this, it's nearly certain to change on you # @return remaining # @api private -- cgit v1.2.1