|
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 an optionally supplied two element |
| 8 | + array of strings. The first and second element of the optional |
| 9 | + array represent what true and false will be converted to respectively. |
| 10 | +
|
8 | 11 | Requires a single boolean as an input. |
9 | 12 | EOS |
10 | 13 | ) do |arguments| |
11 | 14 |
|
12 | 15 | raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " + |
13 | | - "given (#{arguments.size} for 1)") if arguments.size < 1 |
| 16 | + "given (#{arguments.size} for 2)") if arguments.size > 2 |
14 | 17 |
|
15 | 18 | value = arguments[0] |
| 19 | + conversion_names = arguments[1] || ['true', 'false'] |
16 | 20 | klass = value.class |
17 | 21 |
|
18 | 22 | # We can have either true or false, and nothing else |
19 | 23 | unless [FalseClass, TrueClass].include?(klass) |
20 | 24 | raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with') |
21 | 25 | end |
22 | 26 |
|
23 | | - return value.to_s |
| 27 | + unless conversion_names.kind_of?(Array) |
| 28 | + raise(Puppet::ParseError, 'bool2str(): Requires an array to work with') |
| 29 | + end |
| 30 | + |
| 31 | + unless conversion_names.count == 2 |
| 32 | + raise(Puppet::ParseError, 'bool2str(): Requires a two element array to work with') |
| 33 | + end |
| 34 | + |
| 35 | + unless conversion_names.all?{|name| name.kind_of?(String)} |
| 36 | + raise(Puppet::ParseError, "bool2str(): Requires strings to convert to" ) |
| 37 | + end |
| 38 | + |
| 39 | + return value ? conversion_names[0] : conversion_names[1] |
24 | 40 | end |
25 | 41 | end |
26 | 42 |
|
|
0 commit comments