Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / bool2str.rb
1 #
2 # bool2str.rb
3 #
4
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'.
11
12     *Examples:*
13
14     bool2str(true)                    => 'true'
15     bool2str(true, 'yes', 'no')       => 'yes'
16     bool2str(false, 't', 'f')         => 'f'
17
18     Requires a single boolean as an input.
19     EOS
20   ) do |arguments|
21
22     unless arguments.size == 1 or arguments.size == 3
23       raise(Puppet::ParseError, "bool2str(): Wrong number of arguments given (#{arguments.size} for 3)")
24     end
25
26     value = arguments[0]
27     true_string = arguments[1] || 'true'
28     false_string = arguments[2] || 'false'
29     klass = value.class
30
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')
34     end
35
36     unless [true_string, false_string].all?{|x| x.kind_of?(String)}
37       raise(Puppet::ParseError, "bool2str(): Requires strings to convert to" )
38     end
39
40     return value ? true_string : false_string
41   end
42 end
43
44 # vim: set ts=2 sw=2 et :