Puppet Function: to_phpconfig
- Defined in:
-
lib/puppet/functions/to_phpconfig.rb
- Function type:
- Ruby 4.x API
Overview
to_phpconfig(Hash $original_hash) ⇒ Hash
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/puppet/functions/to_phpconfig.rb', line 3
Puppet::Functions.create_function(:to_phpconfig) do
dispatch :convert do
required_param 'Hash', :original_hash
return_type 'Hash'
end
def convert(original_hash)
new_hash = {}
original_hash.each do |org_key, org_val|
new_key = '$config'
key_split = org_key.split('.')
new_key += format_key(key_split)
case org_val.to_s.strip
when %r{^(\d+\.\d+|\d+|true|false)$}
new_hash[new_key] = org_val
when %r{^(\[\{.*\}\]|\{.*\}\,(\{.*\}\,?)*)$}
num = 0
org_val.each do |hash|
new_hash["#{new_key}[#{num}]"] = format_val_hash(hash)
num += 1
end
when %r{^\[.*\]$}
new_hash[new_key] = format_val_array(org_val)
when %r{^\{.*\}$}
new_hash[new_key] = format_val_hash(org_val)
else
new_hash[new_key] = "'#{org_val}'"
end
end
new_hash
end
def format_key(values)
formatted_vals = []
values.each do |item|
begin formatted_vals.push(Integer(item))
rescue StandardError
formatted_vals.push("'#{item}'")
end
end
formatted_vals.map! { |item| "[#{item}]" }
formatted_vals.join
end
def format_val_array(values)
joined_vals = values.join("', '")
"array('#{joined_vals}')"
end
def format_val_hash(hash)
formatted_vals = []
hash.each { |key, value| formatted_vals.push("'#{key}' => '#{value}'") }
joined_vals = formatted_vals.join(', ')
"array(#{joined_vals})"
end
end
|