source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/suffix.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.3 KB
Line 
1#
2# suffix.rb
3#
4
5module Puppet::Parser::Functions
6 newfunction(:suffix, :type => :rvalue, :doc => <<-EOS
7This function applies a suffix to all elements in an array, or to the keys
8in a hash.
9
10*Examples:*
11
12 suffix(['a','b','c'], 'p')
13
14Will return: ['ap','bp','cp']
15 EOS
16 ) do |arguments|
17
18 # Technically we support two arguments but only first is mandatory ...
19 raise(Puppet::ParseError, "suffix(): Wrong number of arguments " +
20 "given (#{arguments.size} for 1)") if arguments.size < 1
21
22 enumerable = arguments[0]
23
24 unless enumerable.is_a?(Array) or enumerable.is_a?(Hash)
25 raise Puppet::ParseError, "suffix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}"
26 end
27
28 suffix = arguments[1] if arguments[1]
29
30 if suffix
31 unless suffix.is_a? String
32 raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}"
33 end
34 end
35
36 if enumerable.is_a?(Array)
37 # Turn everything into string same as join would do ...
38 result = enumerable.collect do |i|
39 i = i.to_s
40 suffix ? i + suffix : i
41 end
42 else
43 result = Hash[enumerable.map do |k,v|
44 k = k.to_s
45 [ suffix ? k + suffix : k, v ]
46 end]
47 end
48
49 return result
50 end
51end
52
53# vim: set ts=2 sw=2 et :
Note: See TracBrowser for help on using the repository browser.