]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/lib/puppet/parser/functions/num2bool.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / modules / stdlib / lib / puppet / parser / functions / num2bool.rb
1 #
2 # num2bool.rb
3 #
4
5 # TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ...
6
7 module Puppet::Parser::Functions
8   newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS
9 This function converts a number into a true boolean. Zero becomes false. Numbers
10 higher then 0 become true.
11     EOS
12   ) do |arguments|
13
14     raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " +
15       "given (#{arguments.size} for 1)") if arguments.size < 1
16
17     number = arguments[0]
18
19     # Only numbers allowed ...
20     unless number.match(/^\-?\d+$/)
21       raise(Puppet::ParseError, 'num2bool(): Requires integer to work with')
22     end
23
24     result = case number
25       when /^0$/
26         false
27       when /^\-?\d+$/
28         # Numbers in Puppet are often string-encoded which is troublesome ...
29         number = number.to_i
30         # We yield true for any positive number and false otherwise ...
31         number > 0 ? true : false
32       else
33         raise(Puppet::ParseError, 'num2bool(): Unknown numeric format given')
34     end
35
36     return result
37   end
38 end
39
40 # vim: set ts=2 sw=2 et :