5 module Puppet::Parser::Functions
6 newfunction(:suffix, :type => :rvalue, :doc => <<-EOS
7 This function applies a suffix to all elements in an array, or to the keys
12 suffix(['a','b','c'], 'p')
14 Will return: ['ap','bp','cp']
18 # Technically we support two arguments but only first is mandatory ...
19 raise(Puppet::ParseError, "suffix(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1
21 enumerable = arguments[0]
23 unless enumerable.is_a?(Array) or enumerable.is_a?(Hash)
24 raise Puppet::ParseError, "suffix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}"
27 suffix = arguments[1] if arguments[1]
30 unless suffix.is_a? String
31 raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}"
35 if enumerable.is_a?(Array)
36 # Turn everything into string same as join would do ...
37 result = enumerable.collect do |i|
39 suffix ? i + suffix : i
42 result = Hash[enumerable.map do |k,v|
44 [ suffix ? k + suffix : k, v ]
52 # vim: set ts=2 sw=2 et :