Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / functions / sprintf_hash.rb
1 # @summary
2 #  Uses sprintf with named references.
3 #
4 # The first parameter is format string describing how the rest of the parameters in the hash
5 # should be formatted. See the documentation for the `Kernel::sprintf` function in Ruby for
6 # all the details.
7 #
8 # In the given argument hash with parameters, all keys are converted to symbols so they work
9 # with the `sprintf` function.
10 #
11 # @example Format a string and number
12 #   $output = sprintf_hash('String: %<foo>s / number converted to binary: %<number>b',
13 #                          { 'foo' => 'a string', 'number' => 5 })
14 #   # $output = 'String: a string / number converted to binary: 101'
15 #
16 # Note that since Puppet 4.10.10, and 5.3.4 this functionality is supported by the
17 # `sprintf` function in puppet core.
18 #
19 Puppet::Functions.create_function(:sprintf_hash) do
20   # @param format The format to use.
21   # @param arguments Hash with parameters.
22   # @return The formatted string.
23   dispatch :sprintf_hash do
24     param 'String', :format
25     param 'Hash', :arguments
26     # Disabled for now. This gives issues on puppet 4.7.1.
27     # return_type 'String'
28   end
29
30   def sprintf_hash(format, arguments)
31     call_function('deprecation', 'sprintf_hash', 'This method is deprecated. From Puppet 4.10.10/5.3.4 please use the built-in sprintf instead')
32
33     Kernel.sprintf(format, Hash[arguments.map { |(k, v)| [k.to_sym, v] }])
34   end
35 end