From 1c07b8dba921f9ecc73fa29d9994b9ccf07e8538 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 03:09:52 +0100 Subject: First version. Simple abs function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- abs.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 abs.rb diff --git a/abs.rb b/abs.rb new file mode 100644 index 0000000..694bff9 --- /dev/null +++ b/abs.rb @@ -0,0 +1,32 @@ +# +# abs.rb +# + +module Puppet::Parser::Functions + newfunction(:abs, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "abs(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + + if value.is_a?(String) + if value.match(/^-?(?:\d+)(?:\.\d+){1}$/) + value = value.to_f + elsif value.match(/^-?\d+$/) + value = value.to_i + else + raise(Puppet::ParseError, 'abs(): Requires a numeric ' + + 'value to work with') + end + end + + result = value.abs + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3