]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/lib/puppet/parser/functions/prefix.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / modules / stdlib / lib / puppet / parser / functions / prefix.rb
1 #
2 # prefix.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:prefix, :type => :rvalue, :doc => <<-EOS
7 This function applies a prefix to all elements in an array.
8
9 *Examles:*
10
11     prefix(['a','b','c'], 'p')
12
13 Will return: ['pa','pb','pc']
14     EOS
15   ) do |arguments|
16
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
20
21     array = arguments[0]
22
23     unless array.is_a?(Array)
24       raise(Puppet::ParseError, 'prefix(): Requires array to work with')
25     end
26
27     prefix = arguments[1] if arguments[1]
28
29     if prefix
30       unless prefix.is_a?(String)
31         raise(Puppet::ParseError, 'prefix(): Requires string to work with')
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       prefix ? prefix + i : i
39     end
40
41     return result
42   end
43 end
44
45 # vim: set ts=2 sw=2 et :