comparison modules/stdlib/lib/puppet/parser/functions/any2bool.rb @ 272:c42fb28cff86

Update to a newer Python module This also pulls in an EPEL module (which we don't use) and a newer stdlib version.
author IBBoard <dev@ibboard.co.uk>
date Fri, 03 Jan 2020 19:56:04 +0000
parents
children d9352a684e62
comparison
equal deleted inserted replaced
271:c62728474654 272:c42fb28cff86
1 #
2 # any2bool.rb
3 #
4 module Puppet::Parser::Functions
5 newfunction(:any2bool, :type => :rvalue, :doc => <<-DOC
6 This converts 'anything' to a boolean. In practise it does the following:
7
8 * Strings such as Y,y,1,T,t,TRUE,yes,'true' will return true
9 * Strings such as 0,F,f,N,n,FALSE,no,'false' will return false
10 * Booleans will just return their original value
11 * Number (or a string representation of a number) > 0 will return true, otherwise false
12 * undef will return false
13 * Anything else will return true
14 DOC
15 ) do |arguments|
16
17 raise(Puppet::ParseError, "any2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
18
19 # If argument is already Boolean, return it
20 if !!arguments[0] == arguments[0] # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean
21 return arguments[0]
22 end
23
24 arg = arguments[0]
25
26 if arg.nil?
27 return false
28 end
29
30 if arg == :undef
31 return false
32 end
33
34 valid_float = begin
35 !!Float(arg) # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean
36 rescue
37 false
38 end
39
40 if arg.is_a?(Numeric)
41 return function_num2bool([arguments[0]])
42 end
43
44 if arg.is_a?(String)
45 return function_num2bool([arguments[0]]) if valid_float
46 return function_str2bool([arguments[0]])
47 end
48
49 return true
50 end
51 end
52
53 # vim: set ts=2 sw=2 et :