Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / any2bool.rb
1 #
2 # any2bool.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:any2bool, :type => :rvalue, :doc => <<-EOS
7 This 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 given (#{arguments.size} for 1)") if arguments.size < 1
19
20     # If argument is already Boolean, return it
21     if !!arguments[0] == arguments[0]
22       return arguments[0]
23     end
24
25     arg = arguments[0]
26
27     if arg == nil
28       return false
29     end
30
31     if arg == :undef
32       return false
33     end
34
35     valid_float = !!Float(arg) rescue false
36
37     if arg.is_a?(Numeric)
38       return function_num2bool( [ arguments[0] ] )
39     end
40
41     if arg.is_a?(String)
42       if valid_float
43         return function_num2bool( [ arguments[0] ] )
44       else
45         return function_str2bool( [ arguments[0] ] )
46       end
47     end
48
49     return true
50
51   end
52 end
53
54 # vim: set ts=2 sw=2 et :