diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2018-02-12 17:02:18 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2018-02-13 10:29:13 -0800 |
commit | b671a8036b6c00461ecb97dbb51e50899bac7109 (patch) | |
tree | 4b2413ad8f9c85615b46bc4f488b5daffb84690b | |
parent | 7bcbad36612e48d1f53fc40e3b634ebcb12a9e93 (diff) | |
download | chef-b671a8036b6c00461ecb97dbb51e50899bac7109.tar.gz |
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 <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/node_map.rb | 23 |
1 files changed, 23 insertions, 0 deletions
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 |