Skip to content

Commit c3989c4

Browse files
committed
Namespace function to_ruby()
1 parent fa15755 commit c3989c4

File tree

3 files changed

+47
-33
lines changed

3 files changed

+47
-33
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Convert an object into a String containing its Ruby representation
5+
#
6+
# @example how to output Ruby
7+
# # output Ruby to a file
8+
# $listen = '0.0.0.0'
9+
# $port = 8000
10+
# file { '/opt/acme/etc/settings.rb':
11+
# content => inline_epp(@("SETTINGS")),
12+
# LISTEN = <%= stdlib::to_ruby($listen) %>
13+
# PORT = <%= stdlib::to_ruby($mailserver) %>
14+
# | SETTINGS
15+
# }
16+
17+
Puppet::Functions.create_function(:'stdlib::to_ruby') do
18+
# @param object
19+
# The object to be converted
20+
#
21+
# @return [String]
22+
# The String representation of the object
23+
dispatch :to_ruby do
24+
param 'Any', :object
25+
end
26+
27+
def to_ruby(object)
28+
serialized = Puppet::Pops::Serialization::ToDataConverter.convert(object, rich_data: true)
29+
serialized_to_ruby(serialized)
30+
end
31+
32+
def serialized_to_ruby(serialized)
33+
case serialized
34+
when Array then "[#{serialized.map { |x| serialized_to_ruby(x) }.join(', ')}]"
35+
when Hash then "{#{serialized.map { |k, v| "#{serialized_to_ruby(k)} => #{serialized_to_ruby(v)}" }.join(', ')}}"
36+
else serialized.inspect
37+
end
38+
end
39+
end

lib/puppet/functions/to_ruby.rb

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,14 @@
11
# frozen_string_literal: true
22

3-
# @summary
4-
# Convert an object into a String containing its Ruby representation
5-
#
6-
# @example how to output Ruby
7-
# # output Ruby to a file
8-
# $listen = '0.0.0.0'
9-
# $port = 8000
10-
# file { '/opt/acme/etc/settings.rb':
11-
# content => inline_epp(@("SETTINGS")),
12-
# LISTEN = <%= $listen.to_ruby %>
13-
# PORT = <%= $mailserver.to_ruby %>
14-
# | SETTINGS
15-
# }
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
164

5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::to_ruby`](#stdlibto_ruby) instead.
176
Puppet::Functions.create_function(:to_ruby) do
18-
# @param object
19-
# The object to be converted
20-
#
21-
# @return [String]
22-
# The String representation of the object
23-
dispatch :to_ruby do
24-
param 'Any', :object
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
259
end
26-
27-
def to_ruby(object)
28-
serialized = Puppet::Pops::Serialization::ToDataConverter.convert(object, rich_data: true)
29-
serialized_to_ruby(serialized)
30-
end
31-
32-
def serialized_to_ruby(serialized)
33-
case serialized
34-
when Array then "[#{serialized.map { |x| serialized_to_ruby(x) }.join(', ')}]"
35-
when Hash then "{#{serialized.map { |k, v| "#{serialized_to_ruby(k)} => #{serialized_to_ruby(v)}" }.join(', ')}}"
36-
else serialized.inspect
37-
end
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'to_ruby', 'This function is deprecated, please use stdlib::to_ruby instead.')
12+
call_function('stdlib::to_ruby', *args)
3813
end
3914
end

spec/functions/to_ruby_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'spec_helper'
44

5-
describe 'to_ruby' do
5+
describe 'stdlib::to_ruby' do
66
it { is_expected.not_to be_nil }
77
it { is_expected.to run.with_params('').and_return('""') }
88
it { is_expected.to run.with_params(nil).and_return('nil') }

0 commit comments

Comments
 (0)