Skip to content

Commit a2658ba

Browse files
committed
Namespace function to_json()
1 parent ac267fd commit a2658ba

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
require 'json'
4+
# @summary
5+
# Convert a data structure and output to JSON
6+
Puppet::Functions.create_function(:'stdlib::to_json') do
7+
# @param data
8+
# Data structure which needs to be converted into JSON
9+
#
10+
# @example Output JSON to a file
11+
# file { '/tmp/my.json':
12+
# ensure => file,
13+
# content => stdlib::to_json($myhash),
14+
# }
15+
#
16+
# @return [String] Converted data to JSON
17+
dispatch :to_json do
18+
param 'Any', :data
19+
end
20+
21+
def to_json(data)
22+
data.to_json
23+
end
24+
end

lib/puppet/functions/to_json.rb

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
# frozen_string_literal: true
22

3-
require 'json'
4-
# @summary
5-
# Convert a data structure and output to JSON
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
4+
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::to_json`](#stdlibto_json) instead.
66
Puppet::Functions.create_function(:to_json) do
7-
# @param data
8-
# Data structure which needs to be converted into JSON
9-
#
10-
# @example Output JSON to a file
11-
# file { '/tmp/my.json':
12-
# ensure => file,
13-
# content => to_json($myhash),
14-
# }
15-
#
16-
# @return [String] Converted data to JSON
17-
dispatch :to_json do
18-
param 'Any', :data
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
199
end
20-
21-
def to_json(data)
22-
data.to_json
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'to_json', 'This method is deprecated, please use stdlib::to_json instead.')
12+
call_function('stdlib::to_json', *args)
2313
end
2414
end

spec/functions/to_json_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_json' do
5+
describe 'stdlib::to_json' do
66
it { is_expected.not_to eq(nil) }
77
it { is_expected.to run.with_params('').and_return('""') }
88
it { is_expected.to run.with_params(true).and_return('true') }

0 commit comments

Comments
 (0)