Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / bool2num.rb
1 #
2 # bool2num.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:bool2num, :type => :rvalue, :doc => <<-DOC
6     @summary
7       Converts a boolean to a number.
8
9     Converts the values:
10       ```
11       false, f, 0, n, and no to 0
12       true, t, 1, y, and yes to 1
13       ```
14     Requires a single boolean or string as an input.
15
16     > *Note:*
17       since Puppet 5.0.0 it is possible to create new data types for almost any
18       datatype using the type system and the built-in
19       [`Numeric.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-numeric),
20       [`Integer.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-integer), and
21       [`Float.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-float)
22       function are used to convert to numeric values.
23       ```
24       notice(Integer(false)) # Notices 0
25       notice(Float(true))    # Notices 1.0
26       ```
27
28     @return [Integer] The converted value as a number
29     DOC
30              ) do |arguments|
31
32     raise(Puppet::ParseError, "bool2num(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
33
34     value = function_str2bool([arguments[0]])
35
36     # We have real boolean values as well ...
37     result = value ? 1 : 0
38
39     return result
40   end
41 end
42
43 # vim: set ts=2 sw=2 et :