summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown4
-rw-r--r--lib/puppet/parser/functions/join_keys_to_values.rb15
-rwxr-xr-xspec/functions/join_keys_to_values_spec.rb4
3 files changed, 19 insertions, 4 deletions
diff --git a/README.markdown b/README.markdown
index b41039d..cc29af9 100644
--- a/README.markdown
+++ b/README.markdown
@@ -772,7 +772,9 @@ Joins an array into a string using a separator. For example, `join(['a','b','c']
#### `join_keys_to_values`
-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 which each element is one joined key/value pair. For example, `join_keys_to_values({'a'=>1,'b'=>2}, " is ")` results in ["a is 1","b is 2"]. *Type*: rvalue.
+Joins each key of a hash to that key's corresponding value with a 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. For example, `join_keys_to_values({'a'=>1,'b'=>[2,3]}, " is ")` results in ["a is 1","b is 2","b is 3"]. *Type*: rvalue.
#### `keys`
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
diff --git a/spec/functions/join_keys_to_values_spec.rb b/spec/functions/join_keys_to_values_spec.rb
index 6c109d1..c2bae5b 100755
--- a/spec/functions/join_keys_to_values_spec.rb
+++ b/spec/functions/join_keys_to_values_spec.rb
@@ -16,4 +16,8 @@ describe 'join_keys_to_values' do
result = subject.call([{ 'key1' => 'value1', 'key2' => 'value2' }, ':'])
expect(result.sort).to eq(['key1:value1', 'key2:value2'].sort)
end
+ it 'should run join_keys_to_values(<hash with array value>, " ") and return the proper array' do
+ result = subject.call([{ 'key1' => 'value1', 'key2' => ['value2', 'value3'] }, ' '])
+ expect(result.sort).to eq(['key1 value1', 'key2 value2', 'key2 value3'].sort)
+ end
end