diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2018-02-12 17:02:18 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2018-02-12 17:02:18 -0800 |
commit | 4aa6f211880a840721104592c0b65b6959cec88d (patch) | |
tree | 98e3e2f50d293f22776934b83d71ad8988ee00ef /lib/chef | |
parent | af4db730bb7ae2e03b6fd6ce7d2b74d2ef2e3336 (diff) | |
download | chef-4aa6f211880a840721104592c0b65b6959cec88d.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>
Diffstat (limited to 'lib/chef')
-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 ecd5c9df8f..4de1526b8a 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 |