4 module Puppet::Parser::Functions
5 newfunction(:prefix, :type => :rvalue, :doc => <<-DOC
7 This function applies a prefix to all elements in an array or a hash.
11 prefix(['a','b','c'], 'p')
12 Will return: ['pa','pb','pc']
14 > *Note:* since Puppet 4.0.0 the general way to modify values is in array is by using the map
15 function in Puppet. This example does the same as the example above:
16 ['a', 'b', 'c'].map |$x| { "p${x}" }
18 @return [Hash] or [Array] The passed values now contains the passed prefix
22 # Technically we support two arguments but only first is mandatory ...
23 raise(Puppet::ParseError, "prefix(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
25 enumerable = arguments[0]
27 unless enumerable.is_a?(Array) || enumerable.is_a?(Hash)
28 raise Puppet::ParseError, "prefix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}"
31 prefix = arguments[1] if arguments[1]
34 unless prefix.is_a?(String)
35 raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{prefix.inspect}"
39 result = if enumerable.is_a?(Array)
40 # Turn everything into string same as join would do ...
43 prefix ? prefix + i : i
46 Hash[enumerable.map do |k, v|
48 [prefix ? prefix + k : k, v]
56 # vim: set ts=2 sw=2 et :