diff --git a/lib/puppet/functions/to_yaml.rb b/lib/puppet/functions/to_yaml.rb index d5fe259..fe8ec50 100644 --- a/lib/puppet/functions/to_yaml.rb +++ b/lib/puppet/functions/to_yaml.rb @@ -1,22 +1,29 @@ require 'yaml' # @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 4f9ae4b..eecb8a4 100644 --- a/spec/functions/to_yaml_spec.rb +++ b/spec/functions/to_yaml_spec.rb @@ -1,20 +1,22 @@ require 'spec_helper' describe 'to_yaml' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params('').and_return("--- ''\n") } it { is_expected.to run.with_params(true).and_return(%r{--- true\n}) } it { is_expected.to run.with_params('one').and_return(%r{--- one\n}) } it { is_expected.to run.with_params([]).and_return("--- []\n") } it { is_expected.to run.with_params(['one']).and_return("---\n- one\n") } it { is_expected.to run.with_params(['one', 'two']).and_return("---\n- one\n- two\n") } it { is_expected.to run.with_params({}).and_return("--- {}\n") } it { is_expected.to run.with_params('key' => 'value').and_return("---\nkey: value\n") } it { is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) .and_return("---\none:\n oneA: A\n oneB:\n oneB1: '1'\n oneB2: '2'\ntwo:\n- twoA\n- twoB\n") } 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