Skip to content

Commit 4bad315

Browse files
committed
Added new function sprintf_hash to allow using named references
1 parent 2339ea8 commit 4bad315

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,24 @@ Returns the number of elements in a string, an array or a hash. This function wi
16491649
16501650
*Type*: rvalue.
16511651
1652+
#### `sprintf_hash`
1653+
1654+
Perform printf-style formatting with named references of text.
1655+
1656+
The first paameter is format string describing how the rest of the parameters in the hash
1657+
should be formatted. See the documentation for the `Kernel::sprintf` function in Ruby for
1658+
all the details.
1659+
1660+
*Example:*
1661+
1662+
```puppet
1663+
$output = sprintf_hash('String: %<foo>s / number converted to binary: %<number>b',
1664+
{ 'foo' => 'a string', 'number' => 5 })
1665+
# $output = 'String: a string / number converted to binary: 101'
1666+
```
1667+
1668+
*Type*: rvalue
1669+
16521670
#### `sort`
16531671
16541672
Sorts strings and arrays lexically.

lib/puppet/functions/sprintf_hash.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Uses sprintf with named references.
2+
#
3+
# The first parameter is format string describing how the rest of the parameters in the hash
4+
# should be formatted. See the documentation for the `Kernel::sprintf` function in Ruby for
5+
# all the details.
6+
#
7+
# In the given argument hash with parameters, all keys are converted to symbols so they work
8+
# with the `sprintf` function.
9+
#
10+
# @example Format a string and number
11+
# $output = sprintf_hash('String: %<foo>s / number converted to binary: %<number>b',
12+
# { 'foo' => 'a string', 'number' => 5 })
13+
# # $output = 'String: a string / number converted to binary: 101'
14+
#
15+
Puppet::Functions.create_function(:sprintf_hash) do
16+
# @param format The format to use.
17+
# @param arguments Hash with parameters.
18+
# @return The formatted string.
19+
dispatch :sprintf_hash do
20+
param 'String', :format
21+
param 'Hash', :arguments
22+
return_type 'String'
23+
end
24+
25+
def sprintf_hash(format, arguments)
26+
Kernel.sprintf(format, Hash[arguments.map { |(k, v)| [k.to_sym, v] }])
27+
end
28+
end

spec/functions/sprintf_hash_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'spec_helper'
2+
3+
describe 'sprintf_hash' do
4+
it 'exists' do
5+
is_expected.not_to eq(nil)
6+
end
7+
8+
context 'validate param count' do
9+
it 'fails with no arguments' do
10+
is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects 2 arguments}i)
11+
end
12+
it 'fails with 1 argument' do
13+
is_expected.to run.with_params('').and_raise_error(ArgumentError, %r{expects 2 arguments}i)
14+
end
15+
it 'fails with too many arguments' do
16+
is_expected.to run.with_params('', '', '').and_raise_error(ArgumentError, %r{expects 2 arguments}i)
17+
end
18+
end
19+
20+
context 'validate param type' do
21+
it 'fails with wrong format type' do
22+
is_expected.to run.with_params(false, {}).and_raise_error(ArgumentError, %r{parameter 'format' expects a String value}i)
23+
end
24+
it 'fails with wrong arguments type' do
25+
is_expected.to run.with_params('', false).and_raise_error(ArgumentError, %r{parameter 'arguments' expects a Hash value}i)
26+
end
27+
end
28+
29+
it 'prints formats with name placeholders' do
30+
is_expected.to run.with_params('string %<foo>s and integer %<bar>b', 'foo' => '_foo_', 'bar' => 5).and_return('string _foo_ and integer 101')
31+
end
32+
end

0 commit comments

Comments
 (0)