Skip to content

Commit 706b9e8

Browse files
committed
Merge pull request #420 from mhaskel/improveprefix
Add support for hashes in the prefix function
2 parents 20d4939 + 0236cd5 commit 706b9e8

3 files changed

Lines changed: 21 additions & 10 deletions

File tree

README.markdown

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ returns the value of the resource's parameter. For example, the following code r
360360

361361
*Type*: rvalue
362362

363-
* `prefix`: This function applies a prefix to all elements in an array. For example, `prefix(['a','b','c'], 'p')` returns ['pa','pb','pc']. *Type*: rvalue
363+
* `prefix`: This function applies a prefix to all elements in an array or to the keys in a hash. For example, `prefix(['a','b','c'], 'p')` returns ['pa','pb','pc'], and `prefix({'a'=>'b','b'=>'c','c'=>'d'}, 'p')` returns {'pa'=>'b','pb'=>'c','pc'=>'d'}. *Type*: rvalue
364364

365365

366366
* `private`: This function sets the current class or definition as private.
@@ -455,7 +455,6 @@ manifests as a valid password attribute. *Type*: rvalue
455455
* `%Z`: Time zone name
456456
* `%%`: Literal '%' character
457457

458-
459458
* `strip`: This function removes leading and trailing whitespace from a string or from every string inside an array. For example, `strip(" aaa ")` results in "aaa". *Type*: rvalue
460459

461460
* `suffix`: This function applies a suffix to all elements in an array. For example, `suffix(['a','b','c'], 'p')` returns ['ap','bp','cp']. *Type*: rvalue

lib/puppet/parser/functions/prefix.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module Puppet::Parser::Functions
66
newfunction(:prefix, :type => :rvalue, :doc => <<-EOS
7-
This function applies a prefix to all elements in an array.
7+
This function applies a prefix to all elements in an array or a hash.
88
99
*Examples:*
1010
@@ -18,10 +18,10 @@ module Puppet::Parser::Functions
1818
raise(Puppet::ParseError, "prefix(): Wrong number of arguments " +
1919
"given (#{arguments.size} for 1)") if arguments.size < 1
2020

21-
array = arguments[0]
21+
enumerable = arguments[0]
2222

23-
unless array.is_a?(Array)
24-
raise Puppet::ParseError, "prefix(): expected first argument to be an Array, got #{array.inspect}"
23+
unless enumerable.is_a?(Array) or enumerable.is_a?(Hash)
24+
raise Puppet::ParseError, "prefix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}"
2525
end
2626

2727
prefix = arguments[1] if arguments[1]
@@ -32,10 +32,17 @@ module Puppet::Parser::Functions
3232
end
3333
end
3434

35-
# Turn everything into string same as join would do ...
36-
result = array.collect do |i|
37-
i = i.to_s
38-
prefix ? prefix + i : i
35+
if enumerable.is_a?(Array)
36+
# Turn everything into string same as join would do ...
37+
result = enumerable.collect do |i|
38+
i = i.to_s
39+
prefix ? prefix + i : i
40+
end
41+
else
42+
result = Hash[enumerable.map do |k,v|
43+
k = k.to_s
44+
[ prefix ? prefix + k : k, v ]
45+
end]
3946
end
4047

4148
return result

spec/functions/prefix_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@
2525
result = scope.function_prefix([['a','b','c'], 'p'])
2626
expect(result).to(eq(['pa','pb','pc']))
2727
end
28+
29+
it "returns a prefixed hash" do
30+
result = scope.function_prefix([{'a' => 'b','b' => 'c','c' => 'd'}, 'p'])
31+
expect(result).to(eq({'pa'=>'b','pb'=>'c','pc'=>'d'}))
32+
end
2833
end

0 commit comments

Comments
 (0)