4 module Puppet::Parser::Functions
5 newfunction(:bool2str, :type => :rvalue, :doc => <<-DOC
7 Converts a boolean to a string using optionally supplied arguments.
9 The optional second and third arguments represent what true and false will be
10 converted to respectively. If only one argument is given, it will be
11 converted from a boolean to a string containing 'true' or 'false'.
14 The converted value to string of the given Boolean
19 bool2str(true) => 'true'
20 bool2str(true, 'yes', 'no') => 'yes'
21 bool2str(false, 't', 'f') => 'f'
24 Requires a single boolean as an input.
27 since Puppet 5.0.0 it is possible to create new data types for almost any
28 datatype using the type system and the built-in
29 [`String.new`](https://puppet.com/docs/puppet/latest/function.html#boolean-to-string)
30 function is used to convert to String with many different format options.
33 notice(String(false)) # Notices 'false'
34 notice(String(true)) # Notices 'true'
35 notice(String(false, '%y')) # Notices 'yes'
36 notice(String(true, '%y')) # Notices 'no'
41 unless arguments.size == 1 || arguments.size == 3
42 raise(Puppet::ParseError, "bool2str(): Wrong number of arguments given (#{arguments.size} for 3)")
46 true_string = arguments[1] || 'true'
47 false_string = arguments[2] || 'false'
50 # We can have either true or false, and nothing else
51 unless [FalseClass, TrueClass].include?(klass)
52 raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with')
55 unless [true_string, false_string].all? { |x| x.is_a?(String) }
56 raise(Puppet::ParseError, 'bool2str(): Requires strings to convert to')
59 return value ? true_string : false_string
63 # vim: set ts=2 sw=2 et :