source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/bool2str.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 8 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 1.4 KB
Line 
1#
2# bool2str.rb
3#
4
5module 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
43end
44
45# vim: set ts=2 sw=2 et :
Note: See TracBrowser for help on using the repository browser.