1d9189d860
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.
29 lines
674 B
Ruby
29 lines
674 B
Ruby
#
|
|
# union.rb
|
|
#
|
|
|
|
module Puppet::Parser::Functions
|
|
newfunction(:union, :type => :rvalue, :doc => <<-EOS
|
|
This function returns a union of two or more arrays.
|
|
|
|
*Examples:*
|
|
|
|
union(["a","b","c"],["b","c","d"])
|
|
|
|
Would return: ["a","b","c","d"]
|
|
EOS
|
|
) do |arguments|
|
|
|
|
# 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
|
|
|
|
arguments.each do |argument|
|
|
raise(Puppet::ParseError, 'union(): Every parameter must be an array') unless argument.is_a?(Array)
|
|
end
|
|
|
|
arguments.reduce(:|)
|
|
end
|
|
end
|
|
|
|
# vim: set ts=2 sw=2 et :
|