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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/puppet/functions/hash2properties.rb', line 2
Puppet::Functions.create_function(:hash2properties) do
dispatch :properties do
param 'Hash', :input
optional_param 'Hash', :options
end
def properties(input, options = {})
settings = {
'header' => '# THIS FILE IS CONTROLLED BY PUPPET',
'key_val_separator' => '=',
'quote_char' => '',
'list_separator' => ',',
}
settings.merge!(options)
properties_str = []
key_hashes = input.to_a
properties = {}
list_separator = settings['list_separator']
until key_hashes.empty?
key_value = key_hashes.pop
if key_value[1].is_a?(Hash)
key_hashes += key_value[1].to_a.map { |key, value| ["#{key_value[0]}.#{key}", value] }
else
prop_value = if key_value[1].is_a?(Array)
key_value[1].join(list_separator)
else
prop_value = key_value[1]
end
properties[key_value[0]] = prop_value
end
end
key_val_separator = settings['key_val_separator']
quote_char = settings['quote_char']
properties.each do |property, value|
properties_str << "#{property}#{key_val_separator}#{quote_char}#{value}#{quote_char}"
end
properties_str.sort! { |x, y| String(x) <=> String(y) }
properties_str.insert(0, settings['header'], '')
properties_str << ''
properties_str.join("\n")
end
end
|