forked from puppetlabs/puppetlabs-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_values.rb
More file actions
31 lines (22 loc) · 921 Bytes
/
delete_values.rb
File metadata and controls
31 lines (22 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#
# delete_values.rb
#
module Puppet::Parser::Functions
newfunction(:delete_values, :type => :rvalue, :doc => <<-DOC
Deletes all instances of a given value from a hash.
*Examples:*
delete_values({'a'=>'A','b'=>'B','c'=>'C','B'=>'D'}, 'B')
Would return: {'a'=>'A','c'=>'C','B'=>'D'}
Note that since Puppet 4.0.0 the equivalent can be performed with the filter() function in Puppet:
$array.filter |$val| { $val != 'B' }
$hash.filter |$key, $val| { $val != 'B' }
DOC
) do |arguments|
raise(Puppet::ParseError, "delete_values(): Wrong number of arguments given (#{arguments.size} of 2)") if arguments.size != 2
hash, item = arguments
unless hash.is_a?(Hash)
raise(TypeError, "delete_values(): First argument must be a Hash. Given an argument of class #{hash.class}.")
end
hash.dup.delete_if { |_key, val| item == val }
end
end