Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / delete.rb
1 #
2 # delete.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:delete, :type => :rvalue, :doc => <<-EOS
7 Deletes all instances of a given element from an array, substring from a
8 string, or key from a hash.
9
10 *Examples:*
11
12     delete(['a','b','c','b'], 'b')
13     Would return: ['a','c']
14
15     delete({'a'=>1,'b'=>2,'c'=>3}, 'b')
16     Would return: {'a'=>1,'c'=>3}
17
18     delete({'a'=>1,'b'=>2,'c'=>3}, ['b','c'])
19     Would return: {'a'=>1}
20
21     delete('abracadabra', 'bra')
22     Would return: 'acada'
23   EOS
24   ) do |arguments|
25
26     raise(Puppet::ParseError, "delete(): Wrong number of arguments given #{arguments.size} for 2") unless arguments.size == 2
27
28     collection = arguments[0].dup
29     Array(arguments[1]).each do |item|
30       case collection
31         when Array, Hash
32           collection.delete item
33         when String
34           collection.gsub! item, ''
35         else
36           raise(TypeError, "delete(): First argument must be an Array, String, or Hash. Given an argument of class #{collection.class}.")
37       end
38     end
39     collection
40   end
41 end
42
43 # vim: set ts=2 sw=2 et :