diff options
author | Adrien Thebo <git@somethingsinistral.net> | 2013-04-01 09:37:15 -0700 |
---|---|---|
committer | Adrien Thebo <git@somethingsinistral.net> | 2013-04-01 09:37:15 -0700 |
commit | f90c54e2df6e8cbd8490a59c7429938072dc1b8d (patch) | |
tree | a946ee1edb5862430106b35150d82aef3a61d8fe /lib/puppet/parser/functions | |
parent | 4078a6ff44c3ea8c2a071eec3bd7b02dabc770d7 (diff) | |
parent | c372f177708df4c844337e9901646b7b84b86cd8 (diff) | |
download | puppet-stdlib-f90c54e2df6e8cbd8490a59c7429938072dc1b8d.tar.gz puppet-stdlib-f90c54e2df6e8cbd8490a59c7429938072dc1b8d.tar.bz2 |
Merge pull request #139 from hakamadare/19864_num2bool_match_fix
(19864) num2bool match fix
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r-- | lib/puppet/parser/functions/num2bool.rb | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 874db22..af0e6ed 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -2,38 +2,41 @@ # 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 + "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') + case number + when Numeric + # Yay, it's a number + when String + begin + number = Float(number) + rescue ArgumentError => ex + raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{ex.message}") + end + else + begin + number = number.to_s + rescue NoMethodError => ex + raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{ex.message}") + end 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 + # Truncate Floats + number = number.to_i - return result + # Return true for any positive number and false otherwise + return number > 0 end end |