summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions/delete_values.rb
blob: 2fcbd16cc54d141c96f1591ffe43e22176fbb6c2 (plain)
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
module Puppet::Parser::Functions
  newfunction(:delete_values, :type => :rvalue, :doc => <<-EOS
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'}

      EOS
    ) do |arguments|

    raise(Puppet::ParseError,
          "delete_values(): Wrong number of arguments given " +
          "(#{arguments.size} of 2)") if arguments.size != 2

    hash = arguments[0]
    item = arguments[1]

    if not hash.is_a?(Hash)
      raise(TypeError, "delete_values(): First argument must be a Hash. " + \
                       "Given an" " argument of class #{hash.class}.") 
    end
    hash.delete_if { |key, val| item == val }
  end
end