source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/any2bool.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.2 KB
Line 
1#
2# any2bool.rb
3#
4
5module Puppet::Parser::Functions
6 newfunction(:any2bool, :type => :rvalue, :doc => <<-EOS
7This converts 'anything' to a boolean. In practise it does the following:
8
9* Strings such as Y,y,1,T,t,TRUE,yes,'true' will return true
10* Strings such as 0,F,f,N,n,FALSE,no,'false' will return false
11* Booleans will just return their original value
12* Number (or a string representation of a number) > 0 will return true, otherwise false
13* undef will return false
14* Anything else will return true
15 EOS
16 ) do |arguments|
17
18 raise(Puppet::ParseError, "any2bool(): Wrong number of arguments " +
19 "given (#{arguments.size} for 1)") if arguments.size < 1
20
21 # If argument is already Boolean, return it
22 if !!arguments[0] == arguments[0]
23 return arguments[0]
24 end
25
26 arg = arguments[0]
27
28 if arg == nil
29 return false
30 end
31
32 if arg == :undef
33 return false
34 end
35
36 valid_float = !!Float(arg) rescue false
37
38 if arg.is_a?(Numeric)
39 return function_num2bool( [ arguments[0] ] )
40 end
41
42 if arg.is_a?(String)
43 if valid_float
44 return function_num2bool( [ arguments[0] ] )
45 else
46 return function_str2bool( [ arguments[0] ] )
47 end
48 end
49
50 return true
51
52 end
53end
54
55# vim: set ts=2 sw=2 et :
Note: See TracBrowser for help on using the repository browser.