From fcd00999fa193088e0d825ba794c904e2cf0002b Mon Sep 17 00:00:00 2001 From: Matthias Baur Date: Thu, 12 Nov 2020 12:26:35 +0100 Subject: [PATCH] Allow options injection for to_yaml This makes it possible to inject formatting options into the to_yaml function. --- lib/puppet/functions/to_yaml.rb | 13 ++++++++++--- spec/functions/to_yaml_spec.rb | 2 ++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/puppet/functions/to_yaml.rb b/lib/puppet/functions/to_yaml.rb index d5fe2592e..fe8ec5036 100644 --- a/lib/puppet/functions/to_yaml.rb +++ b/lib/puppet/functions/to_yaml.rb @@ -2,21 +2,28 @@ # @summary # Convert a data structure and output it as YAML # -# @example how to output YAML +# @example How to output YAML # # output yaml to a file # file { '/tmp/my.yaml': # ensure => file, # content => to_yaml($myhash), # } +# @example Use options control the output format +# file { '/tmp/my.yaml': +# ensure => file, +# content => to_yaml($myhash, {indentation: 4}) +# } Puppet::Functions.create_function(:to_yaml) do # @param data + # @param options # # @return [String] dispatch :to_yaml do param 'Any', :data + optional_param 'Hash', :options end - def to_yaml(data) - data.to_yaml + def to_yaml(data, options = {}) + data.to_yaml(options) end end diff --git a/spec/functions/to_yaml_spec.rb b/spec/functions/to_yaml_spec.rb index 4f9ae4b65..eecb8a4e2 100644 --- a/spec/functions/to_yaml_spec.rb +++ b/spec/functions/to_yaml_spec.rb @@ -17,4 +17,6 @@ it { is_expected.to run.with_params('‰').and_return("--- \"‰\"\n") } it { is_expected.to run.with_params('∇').and_return("--- \"∇\"\n") } + + it { is_expected.to run.with_params({ 'foo' => { 'bar' => true, 'baz' => false } }, :indentation => 4).and_return("---\nfoo:\n bar: true\n baz: false\n") } end