summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown7
-rw-r--r--lib/puppet/parser/functions/suffix.rb24
-rwxr-xr-xspec/functions/suffix_spec.rb8
3 files changed, 23 insertions, 16 deletions
diff --git a/README.markdown b/README.markdown
index e346f4f..6cedc2a 100644
--- a/README.markdown
+++ b/README.markdown
@@ -904,7 +904,12 @@ Removes leading and trailing whitespace from a string or from every string insid
#### `suffix`
-Applies a suffix to all elements in an array. For example, `suffix(['a','b','c'], 'p')` returns ['ap','bp','cp']. *Type*: rvalue.
+Applies a suffix to all elements in an array, or to the keys in a hash.
+For example:
+* `suffix(['a','b','c'], 'p')` returns ['ap','bp','cp']
+* `suffix({'a'=>'b','b'=>'c','c'=>'d'}, 'p')` returns {'ap'=>'b','bp'=>'c','cp'=>'d'}.
+
+*Type*: rvalue.
#### `swapcase`
diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb
index f7792d6..2908434 100644
--- a/lib/puppet/parser/functions/suffix.rb
+++ b/lib/puppet/parser/functions/suffix.rb
@@ -4,7 +4,8 @@
module Puppet::Parser::Functions
newfunction(:suffix, :type => :rvalue, :doc => <<-EOS
-This function applies a suffix to all elements in an array.
+This function applies a suffix to all elements in an array, or to the keys
+in a hash.
*Examples:*
@@ -18,10 +19,10 @@ Will return: ['ap','bp','cp']
raise(Puppet::ParseError, "suffix(): Wrong number of arguments " +
"given (#{arguments.size} for 1)") if arguments.size < 1
- array = arguments[0]
+ enumerable = arguments[0]
- unless array.is_a?(Array)
- raise Puppet::ParseError, "suffix(): expected first argument to be an Array, got #{array.inspect}"
+ unless enumerable.is_a?(Array) or enumerable.is_a?(Hash)
+ raise Puppet::ParseError, "suffix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}"
end
suffix = arguments[1] if arguments[1]
@@ -32,10 +33,17 @@ Will return: ['ap','bp','cp']
end
end
- # Turn everything into string same as join would do ...
- result = array.collect do |i|
- i = i.to_s
- suffix ? i + suffix : i
+ if enumerable.is_a?(Array)
+ # Turn everything into string same as join would do ...
+ result = enumerable.collect do |i|
+ i = i.to_s
+ suffix ? i + suffix : i
+ end
+ else
+ result = Hash[enumerable.map do |k,v|
+ k = k.to_s
+ [ suffix ? k + suffix : k, v ]
+ end]
end
return result
diff --git a/spec/functions/suffix_spec.rb b/spec/functions/suffix_spec.rb
index b48a4eb..efba4ab 100755
--- a/spec/functions/suffix_spec.rb
+++ b/spec/functions/suffix_spec.rb
@@ -16,29 +16,23 @@ describe 'suffix' do
it { is_expected.to run.with_params(['one'], 'post').and_return(['onepost']) }
it { is_expected.to run.with_params(['one', 'two', 'three'], 'post').and_return(['onepost', 'twopost', 'threepost']) }
it {
- pending("implementation of Hash functionality matching prefix")
is_expected.to run.with_params({}).and_return({})
}
it {
- pending("implementation of Hash functionality matching prefix")
is_expected.to run.with_params({ 'key1' => 'value1', 2 => 3}).and_return({ 'key1' => 'value1', '2' => 3 })
}
it {
- pending("implementation of Hash functionality matching prefix")
is_expected.to run.with_params({}, '').and_return({})
}
it {
- pending("implementation of Hash functionality matching prefix")
is_expected.to run.with_params({ 'key' => 'value' }, '').and_return({ 'key' => 'value' })
}
it {
- pending("implementation of Hash functionality matching prefix")
is_expected.to run.with_params({ 'key' => 'value' }, 'post').and_return({ 'keypost' => 'value' })
}
it {
- pending("implementation of Hash functionality matching prefix")
is_expected.to run \
- .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'pre') \
+ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'post') \
.and_return({ 'key1post' => 'value1', 'key2post' => 'value2', 'key3post' => 'value3' })
}
end