Puppet Function: to_hash_settings
- Defined in:
- lib/puppet/parser/functions/to_hash_settings.rb
- Function type:
- Ruby 3.x API
Overview
This function converts a +=> value+ hash into a nested hash and can add an id to the outer key. The optional id string as second parameter is prepended to the resource name.
Examples:
to_hash_settings(=> 1, 'b' => 2)
Would return: { 'a' => => 'a', 'value' => 1, 'b' => => 'b', 'value' => 2 }
and:
to_hash_settings(=> 1, 'b' => 2, 'foo')
Would return: { 'foo: a' => => 'a', 'value' => 1, 'foo: b' => => 'b', 'value' => 2 }
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/puppet/parser/functions/to_hash_settings.rb', line 3 newfunction(:to_hash_settings, type: :rvalue, doc: <<-EOS This function converts a +{key => value}+ hash into a nested hash and can add an id to the outer key. The optional id string as second parameter is prepended to the resource name. *Examples:* to_hash_settings({'a' => 1, 'b' => 2}) Would return: { 'a' => {'key' => 'a', 'value' => 1}, 'b' => {'key' => 'b', 'value' => 2} } and: to_hash_settings({'a' => 1, 'b' => 2}, 'foo') Would return: { 'foo: a' => {'key' => 'a', 'value' => 1}, 'foo: b' => {'key' => 'b', 'value' => 2} } EOS ) do |arguments| hash, id = arguments id = (id.nil? ? '' : "#{id}: ") raise(Puppet::ParseError, 'to_hash_settings(): Requires hash to work with') unless hash.is_a?(Hash) return hash.each_with_object({}) do |kv, acc| acc[id + kv[0]] = { 'key' => kv[0], 'value' => kv[1] } end end |