Skip to content

Commit 75fad33

Browse files
committed
Allow options injection for to_yaml
This makes it possible to inject formatting options into the to_yaml function.
1 parent 7c1ae25 commit 75fad33

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/puppet/functions/to_yaml.rb

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22
# @summary
33
# Convert a data structure and output it as YAML
44
#
5-
# @example how to output YAML
5+
# @example How to output YAML
66
# # output yaml to a file
77
# file { '/tmp/my.yaml':
88
# ensure => file,
99
# content => to_yaml($myhash),
1010
# }
11+
# @example Use options control the output format
12+
# file { '/tmp/my.yaml':
13+
# ensure => file,
14+
# content => to_yaml($myhash, {indentation: 4})
15+
# }
1116
Puppet::Functions.create_function(:to_yaml) do
1217
# @param data
18+
# @param options
1319
#
1420
# @return [String]
1521
dispatch :to_yaml do
1622
param 'Any', :data
23+
optional_param 'Hash', :options
1724
end
1825

19-
def to_yaml(data)
20-
data.to_yaml
26+
def to_yaml(data, options = {})
27+
data.to_yaml(options)
2128
end
2229
end

spec/functions/to_yaml_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717

1818
it { is_expected.to run.with_params('‰').and_return("--- \"\"\n") }
1919
it { is_expected.to run.with_params('∇').and_return("--- \"\"\n") }
20+
21+
it { is_expected.to run.with_params({'foo' => {'bar' => true, 'baz' => false}}, indentation: 4).and_return("---\nfoo:\n bar: true\n baz: false\n") }
2022
end

0 commit comments

Comments
 (0)