5 module Puppet::Parser::Functions
6 newfunction(:bool2str, :type => :rvalue, :doc => <<-EOS
7 Converts a boolean to a string using optionally supplied arguments. The
8 optional second and third arguments represent what true and false will be
9 converted to respectively. If only one argument is given, it will be
10 converted from a boolean to a string containing 'true' or 'false'.
14 bool2str(true) => 'true'
15 bool2str(true, 'yes', 'no') => 'yes'
16 bool2str(false, 't', 'f') => 'f'
18 Requires a single boolean as an input.
22 unless arguments.size == 1 or arguments.size == 3
23 raise(Puppet::ParseError, "bool2str(): Wrong number of arguments given (#{arguments.size} for 3)")
27 true_string = arguments[1] || 'true'
28 false_string = arguments[2] || 'false'
31 # We can have either true or false, and nothing else
32 unless [FalseClass, TrueClass].include?(klass)
33 raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with')
36 unless [true_string, false_string].all?{|x| x.kind_of?(String)}
37 raise(Puppet::ParseError, "bool2str(): Requires strings to convert to" )
40 return value ? true_string : false_string
44 # vim: set ts=2 sw=2 et :