]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/lib/puppet/parser/functions/str2bool.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / modules / stdlib / lib / puppet / parser / functions / str2bool.rb
1 #
2 # str2bool.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS
7 This converts a string to a boolean. This attempt to convert strings that 
8 contain things like: y, 1, t, true to 'true' and strings that contain things
9 like: 0, f, n, false, no to 'false'.
10     EOS
11   ) do |arguments|
12
13     raise(Puppet::ParseError, "str2bool(): Wrong number of arguments " +
14       "given (#{arguments.size} for 1)") if arguments.size < 1
15
16     string = arguments[0]
17
18     unless string.is_a?(String)
19       raise(Puppet::ParseError, 'str2bool(): Requires either ' +
20         'string to work with')
21     end
22
23     # We consider all the yes, no, y, n and so on too ...
24     result = case string
25       #
26       # This is how undef looks like in Puppet ...
27       # We yield false in this case.
28       #
29       when /^$/, '' then false # Empty string will be false ...
30       when /^(1|t|y|true|yes)$/  then true
31       when /^(0|f|n|false|no)$/  then false
32       when /^(undef|undefined)$/ then false # This is not likely to happen ...
33       else
34         raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given')
35     end
36
37     return result
38   end
39 end
40
41 # vim: set ts=2 sw=2 et :