]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/suffix.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / suffix.rb
1 #
2 # suffix.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:suffix, :type => :rvalue, :doc => <<-EOS
7 This function applies a suffix to all elements in an array.
8
9 *Examples:*
10
11     suffix(['a','b','c'], 'p')
12
13 Will return: ['ap','bp','cp']
14     EOS
15   ) do |arguments|
16
17     # Technically we support two arguments but only first is mandatory ...
18     raise(Puppet::ParseError, "suffix(): Wrong number of arguments " +
19       "given (#{arguments.size} for 1)") if arguments.size < 1
20
21     array = arguments[0]
22
23     unless array.is_a?(Array)
24       raise Puppet::ParseError, "suffix(): expected first argument to be an Array, got #{array.inspect}"
25     end
26
27     suffix = arguments[1] if arguments[1]
28
29     if suffix
30       unless suffix.is_a? String
31         raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}"
32       end
33     end
34
35     # Turn everything into string same as join would do ...
36     result = array.collect do |i|
37       i = i.to_s
38       suffix ? i + suffix : i
39     end
40
41     return result
42   end
43 end
44
45 # vim: set ts=2 sw=2 et :