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 " +
|
---|
24 | "given (#{arguments.size} for 3)")
|
---|
25 | end
|
---|
26 |
|
---|
27 | value = arguments[0]
|
---|
28 | true_string = arguments[1] || 'true'
|
---|
29 | false_string = arguments[2] || 'false'
|
---|
30 | klass = value.class
|
---|
31 |
|
---|
32 | # We can have either true or false, and nothing else
|
---|
33 | unless [FalseClass, TrueClass].include?(klass)
|
---|
34 | raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with')
|
---|
35 | end
|
---|
36 |
|
---|
37 | unless [true_string, false_string].all?{|x| x.kind_of?(String)}
|
---|
38 | raise(Puppet::ParseError, "bool2str(): Requires strings to convert to" )
|
---|
39 | end
|
---|
40 |
|
---|
41 | return value ? true_string : false_string
|
---|
42 | end
|
---|
43 | end
|
---|
44 |
|
---|
45 | # vim: set ts=2 sw=2 et :
|
---|