4 module Puppet::Parser::Functions
5 newfunction(:suffix, :type => :rvalue, :doc => <<-DOC
6 This function applies a suffix to all elements in an array, or to the keys
11 suffix(['a','b','c'], 'p')
13 Will return: ['ap','bp','cp']
15 Note that since Puppet 4.0.0 the general way to modify values is in array is by using the map
16 function in Puppet. This example does the same as the example above:
18 ['a', 'b', 'c'].map |$x| { "${x}p" }
23 # Technically we support two arguments but only first is mandatory ...
24 raise(Puppet::ParseError, "suffix(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
26 enumerable = arguments[0]
28 unless enumerable.is_a?(Array) || enumerable.is_a?(Hash)
29 raise Puppet::ParseError, "suffix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}"
32 suffix = arguments[1] if arguments[1]
35 unless suffix.is_a? String
36 raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}"
40 result = if enumerable.is_a?(Array)
41 # Turn everything into string same as join would do ...
44 suffix ? i + suffix : i
47 Hash[enumerable.map do |k, v|
49 [suffix ? k + suffix : k, v]
57 # vim: set ts=2 sw=2 et :