summaryrefslogtreecommitdiff
path: root/lib/puppet
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/parser/functions/num2bool.rb35
1 files changed, 11 insertions, 24 deletions
diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb
index 638f693..15dd560 100644
--- a/lib/puppet/parser/functions/num2bool.rb
+++ b/lib/puppet/parser/functions/num2bool.rb
@@ -2,39 +2,26 @@
# num2bool.rb
#
-# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ...
-
module Puppet::Parser::Functions
newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS
-This function converts a number into a true boolean. Zero becomes false. Numbers
-higher then 0 become true.
+This function converts a number or a string representation of a number into a
+true boolean. Zero or anything non-numeric becomes false. Numbers higher then 0
+become true.
EOS
) do |arguments|
raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " +
- "given (#{arguments.size} for 1)") if arguments.size < 1
-
- # Since we're matching against a regex, coerce to String
- number = arguments[0].to_s
-
- # Only numbers allowed ...
- unless number.match(/^\-?\d+$/)
- raise(Puppet::ParseError, 'num2bool(): Requires integer to work with')
- end
+ "given (#{arguments.size} for 1)") if arguments.size != 1
- 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')
+ # we need to get an Integer out of this
+ begin
+ number = arguments[0].to_i
+ rescue NoMethodError
+ raise(Puppet::ParseError, 'num2bool(): Unable to parse number: ' + $!)
end
- return result
+ # Return true for any positive number and false otherwise
+ return number > 0 ? true : false
end
end