2011-04-29 04:09:52 +02:00
|
|
|
#
|
|
|
|
# abs.rb
|
|
|
|
#
|
|
|
|
|
|
|
|
module Puppet::Parser::Functions
|
|
|
|
newfunction(:abs, :type => :rvalue, :doc => <<-EOS
|
2011-07-29 20:22:30 +02:00
|
|
|
Returns the absolute value of a number, for example -34.56 becomes
|
|
|
|
34.56. Takes a single integer and float value as an argument.
|
2011-04-29 04:09:52 +02:00
|
|
|
EOS
|
|
|
|
) do |arguments|
|
|
|
|
|
|
|
|
raise(Puppet::ParseError, "abs(): Wrong number of arguments " +
|
|
|
|
"given (#{arguments.size} for 1)") if arguments.size < 1
|
|
|
|
|
|
|
|
value = arguments[0]
|
|
|
|
|
2011-04-30 03:54:47 +02:00
|
|
|
# Numbers in Puppet are often string-encoded which is troublesome ...
|
2011-04-29 04:09:52 +02:00
|
|
|
if value.is_a?(String)
|
|
|
|
if value.match(/^-?(?:\d+)(?:\.\d+){1}$/)
|
|
|
|
value = value.to_f
|
|
|
|
elsif value.match(/^-?\d+$/)
|
|
|
|
value = value.to_i
|
|
|
|
else
|
2011-04-30 00:15:05 +02:00
|
|
|
raise(Puppet::ParseError, 'abs(): Requires float or ' +
|
|
|
|
'integer to work with')
|
2011-04-29 04:09:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-29 21:58:49 +02:00
|
|
|
# We have numeric value to handle ...
|
2011-04-29 04:09:52 +02:00
|
|
|
result = value.abs
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 et :
|