Mercurial > repos > other > Puppet
annotate modules/stdlib/lib/puppet/parser/functions/any2bool.rb @ 482:d83de9b3a62b default tip
Update hiera.yaml within Puppet config
Forgot that we manage it from here. Now has content to match
new packages
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 30 Aug 2024 16:10:36 +0100 |
parents | adf6fe9bbc17 |
children |
rev | line source |
---|---|
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
1 # frozen_string_literal: true |
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
2 |
272 | 3 # |
4 # any2bool.rb | |
5 # | |
6 module Puppet::Parser::Functions | |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
7 newfunction(:any2bool, type: :rvalue, doc: <<-DOC |
275
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
8 @summary |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
9 Converts 'anything' to a boolean. |
272 | 10 |
275
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
11 In practise it does the following: |
272 | 12 * Strings such as Y,y,1,T,t,TRUE,yes,'true' will return true |
13 * Strings such as 0,F,f,N,n,FALSE,no,'false' will return false | |
14 * Booleans will just return their original value | |
15 * Number (or a string representation of a number) > 0 will return true, otherwise false | |
16 * undef will return false | |
17 * Anything else will return true | |
275
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
18 |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
19 Also see the built-in [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean) |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
20 function. |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
21 |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
22 @return [Boolean] The boolean value of the object that was given |
272 | 23 DOC |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
24 ) do |arguments| |
272 | 25 raise(Puppet::ParseError, "any2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? |
26 | |
27 # If argument is already Boolean, return it | |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
28 return arguments[0] if !!arguments[0] == arguments[0] # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean |
272 | 29 |
30 arg = arguments[0] | |
31 | |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
32 return false if arg.nil? |
272 | 33 |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
34 return false if arg == :undef |
272 | 35 |
36 valid_float = begin | |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
37 !!Float(arg) # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean |
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
38 rescue StandardError |
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
39 false |
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
40 end |
272 | 41 |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
42 return function_num2bool([arguments[0]]) if arg.is_a?(Numeric) |
272 | 43 |
44 if arg.is_a?(String) | |
45 return function_num2bool([arguments[0]]) if valid_float | |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
46 |
272 | 47 return function_str2bool([arguments[0]]) |
48 end | |
49 | |
50 return true | |
51 end | |
52 end | |
53 | |
54 # vim: set ts=2 sw=2 et : |