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 "+
|
---|
27 | "given #{arguments.size} for 2") unless arguments.size == 2
|
---|
28 |
|
---|
29 | collection = arguments[0].dup
|
---|
30 | Array(arguments[1]).each do |item|
|
---|
31 | case collection
|
---|
32 | when Array, Hash
|
---|
33 | collection.delete item
|
---|
34 | when String
|
---|
35 | collection.gsub! item, ''
|
---|
36 | else
|
---|
37 | raise(TypeError, "delete(): First argument must be an Array, " +
|
---|
38 | "String, or Hash. Given an argument of class #{collection.class}.")
|
---|
39 | end
|
---|
40 | end
|
---|
41 | collection
|
---|
42 | end
|
---|
43 | end
|
---|
44 |
|
---|
45 | # vim: set ts=2 sw=2 et :
|
---|