Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / suffix.rb
1 #
2 # suffix.rb
3 #
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
7     in a hash.
8
9     *Examples:*
10
11         suffix(['a','b','c'], 'p')
12
13     Will return: ['ap','bp','cp']
14
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:
17
18         ['a', 'b', 'c'].map |$x| { "${x}p" }
19
20     DOC
21              ) do |arguments|
22
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?
25
26     enumerable = arguments[0]
27
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}"
30     end
31
32     suffix = arguments[1] if arguments[1]
33
34     if suffix
35       unless suffix.is_a? String
36         raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}"
37       end
38     end
39
40     result = if enumerable.is_a?(Array)
41                # Turn everything into string same as join would do ...
42                enumerable.map do |i|
43                  i = i.to_s
44                  suffix ? i + suffix : i
45                end
46              else
47                Hash[enumerable.map do |k, v|
48                  k = k.to_s
49                  [suffix ? k + suffix : k, v]
50                end]
51              end
52
53     return result
54   end
55 end
56
57 # vim: set ts=2 sw=2 et :