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 / str2bool.rb
1 #
2 # str2bool.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:str2bool, :type => :rvalue, :doc => <<-DOC
6     This converts a string to a boolean. This attempt to convert strings that
7     contain things like: Y,y, 1, T,t, TRUE,true to 'true' and strings that contain things
8     like: 0, F,f, N,n, false, FALSE, no to 'false'.
9
10     Note that since Puppet 5.0.0 the Boolean data type can convert strings to a Boolean value.
11     See the function new() in Puppet for details what the Boolean data type supports.
12   DOC
13              ) do |arguments|
14
15     raise(Puppet::ParseError, "str2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
16
17     string = arguments[0]
18
19     # If string is already Boolean, return it
20     if !!string == string # rubocop:disable Style/DoubleNegation : No viable alternative
21       return string
22     end
23
24     unless string.is_a?(String)
25       raise(Puppet::ParseError, 'str2bool(): Requires string to work with')
26     end
27
28     # We consider all the yes, no, y, n and so on too ...
29     result = case string
30              #
31              # This is how undef looks like in Puppet ...
32              # We yield false in this case.
33              #
34              when %r{^$}, '' then false # Empty string will be false ...
35              when %r{^(1|t|y|true|yes)$}i  then true
36              when %r{^(0|f|n|false|no)$}i  then false
37              when %r{^(undef|undefined)$} then false # This is not likely to happen ...
38              else
39                raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given')
40              end
41
42     return result
43   end
44 end
45
46 # vim: set ts=2 sw=2 et :