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'.
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.
15 raise(Puppet::ParseError, "str2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
19 # If string is already Boolean, return it
20 if !!string == string # rubocop:disable Style/DoubleNegation : No viable alternative
24 unless string.is_a?(String)
25 raise(Puppet::ParseError, 'str2bool(): Requires string to work with')
28 # We consider all the yes, no, y, n and so on too ...
31 # This is how undef looks like in Puppet ...
32 # We yield false in this case.
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 ...
39 raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given')
46 # vim: set ts=2 sw=2 et :