-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathto_ruby.rb
More file actions
34 lines (31 loc) · 870 Bytes
/
to_ruby.rb
File metadata and controls
34 lines (31 loc) · 870 Bytes
1
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
# frozen_string_literal: true
# @summary
# Convert an object into a String containing its Ruby representation
#
# @example how to output Ruby
# # output Ruby to a file
# $listen = '0.0.0.0'
# $port = 8000
# file { '/opt/acme/etc/settings.rb':
# content => inline_epp(@("SETTINGS")),
# LISTEN = <%= $listen.to_ruby %>
# PORT = <%= $mailserver.to_ruby %>
# | SETTINGS
# }
Puppet::Functions.create_function(:to_ruby) do
dispatch :to_ruby do
param 'Any', :object
end
# @param object
# The object to be converted
#
# @return [String]
# The String representation of the object
def to_ruby(object)
case object
when Array then "[#{object.map { |x| to_ruby(x) }.join(', ')}]"
when Hash then "{#{object.map { |k, v| "#{to_ruby(k)} => #{to_ruby(v)}" }.join(', ')}}"
else object.inspect
end
end
end