]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/lib/puppet/parser/functions/abs.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / modules / stdlib / lib / puppet / parser / functions / abs.rb
1 #
2 # abs.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:abs, :type => :rvalue, :doc => <<-EOS
7     Returns the absolute value of a number, for example -34.56 becomes 
8     34.56. Takes a single integer and float value as an argument.
9     EOS
10   ) do |arguments|
11
12     raise(Puppet::ParseError, "abs(): Wrong number of arguments " +
13       "given (#{arguments.size} for 1)") if arguments.size < 1
14
15     value = arguments[0]
16
17     # Numbers in Puppet are often string-encoded which is troublesome ...
18     if value.is_a?(String)
19       if value.match(/^-?(?:\d+)(?:\.\d+){1}$/)
20         value = value.to_f
21       elsif value.match(/^-?\d+$/)
22         value = value.to_i
23       else
24         raise(Puppet::ParseError, 'abs(): Requires float or ' +
25           'integer to work with')
26       end
27     end
28
29     # We have numeric value to handle ...
30     result = value.abs
31
32     return result
33   end
34 end
35
36 # vim: set ts=2 sw=2 et :