summaryrefslogtreecommitdiff
path: root/num2bool.rb
diff options
context:
space:
mode:
authorKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>2011-04-25 03:47:31 +0100
committerKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>2011-04-25 03:47:31 +0100
commit7e7c7ce2ee176017620987c36e4dd5abc2dd304b (patch)
treef5e6bf9f52ecc8ba6644539addd5ad674df494be /num2bool.rb
parent1a8617404c393c9efdbebe9d1d9b5c9c54e536de (diff)
First version. Function opposite to the bool2num one. Converts
number to appropriate boolean value. Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Diffstat (limited to 'num2bool.rb')
-rw-r--r--num2bool.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/num2bool.rb b/num2bool.rb
new file mode 100644
index 0000000..0e6b7a5
--- /dev/null
+++ b/num2bool.rb
@@ -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 :