Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / delete_values.rb
1 #
2 # delete_values.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:delete_values, :type => :rvalue, :doc => <<-DOC
6     @summary
7       Deletes all instances of a given value from a hash.
8
9     @example Example usage
10
11       delete_values({'a'=>'A','b'=>'B','c'=>'C','B'=>'D'}, 'B')
12       Would return: {'a'=>'A','c'=>'C','B'=>'D'}
13
14     > *Note:*
15     Since Puppet 4.0.0 the equivalent can be performed with the
16     built-in [`filter`](https://puppet.com/docs/puppet/latest/function.html#filter) function:
17     $array.filter |$val| { $val != 'B' }
18     $hash.filter |$key, $val| { $val != 'B' }
19
20     @return [Hash] The given hash now missing all instances of the targeted value
21     DOC
22              ) do |arguments|
23
24     raise(Puppet::ParseError, "delete_values(): Wrong number of arguments given (#{arguments.size} of 2)") if arguments.size != 2
25
26     hash, item = arguments
27
28     unless hash.is_a?(Hash)
29       raise(TypeError, "delete_values(): First argument must be a Hash. Given an argument of class #{hash.class}.")
30     end
31     hash.dup.delete_if { |_key, val| item == val }
32   end
33 end