Skip to content

to_yaml function added. #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/puppet/parser/functions/to_yaml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# to_yaml.rb
#

module Puppet::Parser::Functions
newfunction(:to_yaml, :type => :rvalue, :doc => <<-EOS
This function accepts a puppet data structure (I.E hash/array/string or nested combination of these) and converts it into a yaml format string, so that YAML files can be written using the content param in the file provider.
EOS
) do |arguments|

if (arguments.size != 1) then
raise(Puppet::ParseError, "to_yaml(): Wrong number of arguments "+
"given #{arguments.size} for 1")
end

ZAML.dump(arguments[0])

end
end

# vim: set ts=2 sw=2 et :
28 changes: 28 additions & 0 deletions spec/functions/to_yaml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'

describe "the to_yaml function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

it "should exist" do
Puppet::Parser::Functions.function("to_yaml").should == "function_to_yaml"
end

it "should raise a ParseError if there is less than 1 arguments" do
lambda { scope.function_to_yaml([]) }.should( raise_error(Puppet::ParseError))
end

it "should convert a data structure to YAML" do
yaml_data = {
'string' => 'a string',
'array' => ['aaa', 'bbb', 'ccc' ],
'bool' => true,
'hash' => {
'string' => 'indeed it is',
'another_string' => 'quite so'
},
}
result = scope.function_to_yaml([yaml_data])
result.should(eq("#{yaml_data.to_yaml}"))
end
end