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 " +
19 "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, "prefix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}"
27 prefix = arguments[1] if arguments[1]
30 unless prefix.is_a?(String)
31 raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{prefix.inspect}"
35 if enumerable.is_a?(Array)
36 # Turn everything into string same as join would do ...
37 result = enumerable.collect do |i|
39 prefix ? prefix + i : i
42 result = Hash[enumerable.map do |k,v|
44 [ prefix ? prefix + k : k, v ]
52 # vim: set ts=2 sw=2 et :