]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
First version. Simple abs function to use within Puppet DSL.
authorKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Fri, 29 Apr 2011 02:09:52 +0000 (03:09 +0100)
committerKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Fri, 29 Apr 2011 02:09:52 +0000 (03:09 +0100)
Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
abs.rb [new file with mode: 0644]

diff --git a/abs.rb b/abs.rb
new file mode 100644 (file)
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 :