Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / num2bool.rb
1 #
2 # num2bool.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:num2bool, :type => :rvalue, :doc => <<-DOC
6     This function converts a number or a string representation of a number into a
7     true boolean. Zero or anything non-numeric becomes false. Numbers higher then 0
8     become true.
9
10     Note that since Puppet 5.0.0 the same can be achieved with the Puppet Type System.
11     See the new() function in Puppet for the many available type conversions.
12
13         Boolean(0) # false
14         Boolean(1) # true
15     DOC
16              ) do |arguments|
17
18     raise(Puppet::ParseError, "num2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1
19
20     number = arguments[0]
21
22     case number
23     when Numeric # rubocop:disable Lint/EmptyWhen : Required for the module to work
24       # Yay, it's a number
25     when String
26       begin
27         number = Float(number)
28       rescue ArgumentError => ex
29         raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{ex.message}")
30       end
31     else
32       begin
33         number = number.to_s
34       rescue NoMethodError => ex
35         raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{ex.message}")
36       end
37     end
38
39     # Truncate Floats
40     number = number.to_i
41
42     # Return true for any positive number and false otherwise
43     return number > 0
44   end
45 end
46
47 # vim: set ts=2 sw=2 et :