summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions
diff options
context:
space:
mode:
authorJetroid <holtjet@gmail.com>2015-08-24 12:01:29 +0100
committerJetroid <holtjet@gmail.com>2015-08-24 14:24:10 +0100
commit1d9189d860f28067b72093cbe4027cf49b7d612c (patch)
tree315a8a912f1bd8d17530b84bdb923b45596cae3d /lib/puppet/parser/functions
parent1d89df906e1ae1d09a862974181663caaf8012c6 (diff)
downloadpuppet-stdlib-1d9189d860f28067b72093cbe4027cf49b7d612c.tar.gz
puppet-stdlib-1d9189d860f28067b72093cbe4027cf49b7d612c.tar.bz2
(MODULE-2456) Modify union to accept more than two arrays
Add spec tests to test the new functionality: *Case for 3 arrays. *Case for 4 arrays. Modify README to note new functionality. This is for issue MODULE-2456, follow the precedent of MODULE-444. This change allows union to be much more useful, unioning many arrays in one line rather than in n lines. Additionally, as this is only added functionality, and does not affect the 2 array case that all modules currently using array are using, it should not affect any existing modules utilizing union. This is now useful, for example, for merging many arrays of resources (eg: packages.) to generate just one list with no duplicates, to avoid duplicate resource declarations.
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r--lib/puppet/parser/functions/union.rb17
1 files changed, 6 insertions, 11 deletions
diff --git a/lib/puppet/parser/functions/union.rb b/lib/puppet/parser/functions/union.rb
index c91bb80..6c5bb83 100644
--- a/lib/puppet/parser/functions/union.rb
+++ b/lib/puppet/parser/functions/union.rb
@@ -4,7 +4,7 @@
module Puppet::Parser::Functions
newfunction(:union, :type => :rvalue, :doc => <<-EOS
-This function returns a union of two arrays.
+This function returns a union of two or more arrays.
*Examples:*
@@ -14,20 +14,15 @@ Would return: ["a","b","c","d"]
EOS
) do |arguments|
- # Two arguments are required
+ # Check that 2 or more arguments have been given ...
raise(Puppet::ParseError, "union(): Wrong number of arguments " +
- "given (#{arguments.size} for 2)") if arguments.size != 2
+ "given (#{arguments.size} for < 2)") if arguments.size < 2
- first = arguments[0]
- second = arguments[1]
-
- unless first.is_a?(Array) && second.is_a?(Array)
- raise(Puppet::ParseError, 'union(): Requires 2 arrays')
+ arguments.each do |argument|
+ raise(Puppet::ParseError, 'union(): Every parameter must be an array') unless argument.is_a?(Array)
end
- result = first | second
-
- return result
+ arguments.reduce(:|)
end
end