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