|
4 | 4 |
|
5 | 5 | module Puppet::Parser::Functions |
6 | 6 | newfunction(:bool2str, :type => :rvalue, :doc => <<-EOS |
7 | | - Converts a boolean to a string. |
| 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 | +
|
8 | 18 | Requires a single boolean as an input. |
9 | 19 | EOS |
10 | 20 | ) do |arguments| |
11 | 21 |
|
12 | | - raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " + |
13 | | - "given (#{arguments.size} for 1)") if arguments.size < 1 |
| 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 |
14 | 26 |
|
15 | 27 | value = arguments[0] |
| 28 | + true_string = arguments[1] || 'true' |
| 29 | + false_string = arguments[2] || 'false' |
16 | 30 | klass = value.class |
17 | 31 |
|
18 | 32 | # We can have either true or false, and nothing else |
19 | 33 | unless [FalseClass, TrueClass].include?(klass) |
20 | 34 | raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with') |
21 | 35 | end |
22 | 36 |
|
23 | | - return value.to_s |
| 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 |
24 | 42 | end |
25 | 43 | end |
26 | 44 |
|
|
0 commit comments