5 module Puppet::Parser::Functions
6 newfunction(:prefix, :type => :rvalue, :doc => <<-EOS
7 This function applies a prefix to all elements in an array or a hash.
11 prefix(['a','b','c'], 'p')
13 Will return: ['pa','pb','pc']
17 # Technically we support two arguments but only first is mandatory ...
18 raise(Puppet::ParseError, "prefix(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1
20 enumerable = arguments[0]
22 unless enumerable.is_a?(Array) or enumerable.is_a?(Hash)
23 raise Puppet::ParseError, "prefix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}"
26 prefix = arguments[1] if arguments[1]
29 unless prefix.is_a?(String)
30 raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{prefix.inspect}"
34 if enumerable.is_a?(Array)
35 # Turn everything into string same as join would do ...
36 result = enumerable.collect do |i|
38 prefix ? prefix + i : i
41 result = Hash[enumerable.map do |k,v|
43 [ prefix ? prefix + k : k, v ]
51 # vim: set ts=2 sw=2 et :