puppetlabs-stdlib/num2bool.rb
Krzysztof Wilczynski 3c4c1c7c20 Moved to unless from if not to improve code clarity. Added TODO
for future reference.  Changed wording of few error messages.

Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
2011-04-30 02:48:25 +01:00

38 lines
990 B
Ruby

#
# num2bool.rb
#
# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ...
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 ...
unless number.match(/^\-?\d+$/)
raise(Puppet::ParseError, 'num2bool(): Requires integer to work with')
end
result = case number
when /^0$/
false
when /^\-?\d+$/
# Numbers in Puppet are often string-encoded which is troublesome ...
number = number.to_i
# We yield true for any positive number and false otherwise ...
number > 0 ? true : false
else
raise(Puppet::ParseError, 'num2bool(): Unknown numeric format given')
end
return result
end
end
# vim: set ts=2 sw=2 et :