Update stdlib and concat to 6.1.0 both
[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     @summary
7       This function converts a number or a string representation of a number into a
8       true boolean.
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     @return [Boolean]
14         Boolean(0) # false for any zero or negative number
15         Boolean(1) # true for any positive number
16     DOC
17              ) do |arguments|
18
19     raise(Puppet::ParseError, "num2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1
20
21     number = arguments[0]
22
23     case number
24     when Numeric # rubocop:disable Lint/EmptyWhen : Required for the module to work
25       # Yay, it's a number
26     when String
27       begin
28         number = Float(number)
29       rescue ArgumentError => ex
30         raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{ex.message}")
31       end
32     else
33       begin
34         number = number.to_s
35       rescue NoMethodError => ex
36         raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{ex.message}")
37       end
38     end
39
40     # Truncate Floats
41     number = number.to_i
42
43     # Return true for any positive number and false otherwise
44     return number > 0
45   end
46 end
47
48 # vim: set ts=2 sw=2 et :