Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / concat.rb
1 #
2 # concat.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:concat, :type => :rvalue, :doc => <<-DOC
6     @summary
7       Appends the contents of multiple arrays into array 1.
8
9     @example Example usage
10
11       concat(['1','2','3'],'4') returns ['1','2','3','4']
12       concat(['1','2','3'],'4',['5','6','7']) returns ['1','2','3','4','5','6','7']
13
14     > *Note:*
15       Since Puppet 4.0, you can use the `+`` operator for concatenation of arrays and
16       merge of hashes, and the `<<`` operator for appending:
17
18     `['1','2','3'] + ['4','5','6'] + ['7','8','9']` returns `['1','2','3','4','5','6','7','8','9']`
19     `[1, 2, 3] << 4` returns `[1, 2, 3, 4]`
20     `[1, 2, 3] << [4, 5]` returns `[1, 2, 3, [4, 5]]`
21
22     @return [Array] The single concatenated array
23   DOC
24              ) do |arguments|
25
26     # Check that more than 2 arguments have been given ...
27     raise(Puppet::ParseError, "concat(): Wrong number of arguments given (#{arguments.size} for < 2)") if arguments.size < 2
28
29     a = arguments[0]
30
31     # Check that the first parameter is an array
32     unless a.is_a?(Array)
33       raise(Puppet::ParseError, 'concat(): Requires array to work with')
34     end
35
36     result = a
37     arguments.shift
38
39     arguments.each do |x|
40       result += (x.is_a?(Array) ? x : [x])
41     end
42
43     return result
44   end
45 end
46
47 # vim: set ts=2 sw=2 et :