]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
First version. Function opposite to the bool2num one. Converts
authorKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Mon, 25 Apr 2011 02:47:31 +0000 (03:47 +0100)
committerKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Mon, 25 Apr 2011 02:47:31 +0000 (03:47 +0100)
number to appropriate boolean value.

Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
num2bool.rb [new file with mode: 0644]

diff --git a/num2bool.rb b/num2bool.rb
new file mode 100644 (file)
index 0000000..0e6b7a5
--- /dev/null
@@ -0,0 +1,36 @@
+#
+# num2bool.rb
+#
+
+module Puppet::Parser::Functions
+  newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS
+    EOS
+  ) do |arguments|
+
+    raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " +
+      "given (#{arguments.size} for 1)") if arguments.size < 1
+
+    number = arguments[0]
+
+    # Only numbers allowed ...
+    if not number.match(/^\-?\d+$/)
+      raise(Puppet::ParseError, 'num2bool(): Requires a number to work with')
+    end
+
+    result = case number
+      when /^0$/
+        false
+      when /^\-?\d+$/
+        # In Puppet numbers are often string-encoded ...
+        number = number.to_i
+        # We yield true for any positive number and false otherwise ...
+        number > 0 ? true : false
+      else
+        raise(Puppet::ParseError, 'num2bool(): Unknown number format given')
+    end
+
+    return result
+  end
+end
+
+# vim: set ts=2 sw=2 et :