source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/delete_regex.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 7 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 1.2 KB
Line 
1#
2# delete_regex.rb
3#
4
5module Puppet::Parser::Functions
6 newfunction(:delete_regex, :type => :rvalue, :doc => <<-EOS
7deletes all instances of a given element that match a regular expression
8from an array or key from a hash. Multiple regular expressions are assumed
9to be matched as an OR.
10
11*Examples:*
12
13 delete_regex(['a','b','c','b'], 'b')
14 Would return: ['a','c']
15
16 delete_regex(['a','b','c','b'], ['b', 'c'])
17 Would return: ['a']
18
19 delete_regex({'a'=>1,'b'=>2,'c'=>3}, 'b')
20 Would return: {'a'=>1,'c'=>3}
21
22 delete_regex({'a'=>1,'b'=>2,'c'=>3}, '^a$')
23 Would return: {'b'=>2,'c'=>3}
24
25 EOS
26 ) do |arguments|
27
28 raise(Puppet::ParseError, "delete_regex(): Wrong number of arguments "+
29 "given #{arguments.size} for 2") unless arguments.size == 2
30
31 collection = arguments[0].dup
32 Array(arguments[1]).each do |item|
33 case collection
34 when Array, Hash, String
35 collection.reject! { |coll_item| (coll_item =~ %r{\b#{item}\b}) }
36 else
37 raise(TypeError, "delete_regex(): First argument must be an Array, " +
38 "Hash, or String. Given an argument of class #{collection.class}.")
39 end
40 end
41 collection
42 end
43end
44
45# vim: set ts=2 sw=2 et :
Note: See TracBrowser for help on using the repository browser.