summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorWilson McCoubrey <wilson@mccoubreys.co.uk>2016-11-10 16:52:30 +0000
committerGitHub <noreply@github.com>2016-11-10 16:52:30 +0000
commitdcef77af4cef2870874f76c06e92f601aeb5bc0c (patch)
tree7116b1977092ce65507661400af791405c171702 /lib
parentb527d3b303fbe8e6deab58f491bc178c761495cd (diff)
parent17a49baae33a78d6aa781aaaadafc08b43def040 (diff)
Merge pull request #632 from MiamiOH/master
Handle array values in join_keys_to_values function
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/parser/functions/join_keys_to_values.rb15
1 files changed, 12 insertions, 3 deletions
diff --git a/lib/puppet/parser/functions/join_keys_to_values.rb b/lib/puppet/parser/functions/join_keys_to_values.rb
index e9924fe..e3baf9f 100644
--- a/lib/puppet/parser/functions/join_keys_to_values.rb
+++ b/lib/puppet/parser/functions/join_keys_to_values.rb
@@ -5,7 +5,8 @@
module Puppet::Parser::Functions
newfunction(:join_keys_to_values, :type => :rvalue, :doc => <<-EOS
This function joins each key of a hash to that key's corresponding value with a
-separator. Keys and values are cast to strings. The return value is an array in
+separator. Keys are cast to strings. If values are arrays, multiple keys
+are added for each element. The return value is an array in
which each element is one joined key/value pair.
*Examples:*
@@ -13,6 +14,10 @@ which each element is one joined key/value pair.
join_keys_to_values({'a'=>1,'b'=>2}, " is ")
Would result in: ["a is 1","b is 2"]
+
+ join_keys_to_values({'a'=>1,'b'=>[2,3]}, " is ")
+
+Would result in: ["a is 1","b is 2","b is 3"]
EOS
) do |arguments|
@@ -38,8 +43,12 @@ Would result in: ["a is 1","b is 2"]
# Join the keys to their values.
hash.map do |k,v|
- String(k) + separator + String(v)
- end
+ if v.is_a?(Array)
+ v.map { |va| String(k) + separator + String(va) }
+ else
+ String(k) + separator + String(v)
+ end
+ end.flatten
end
end