2011-04-25 04:47:31 +02:00
|
|
|
#
|
|
|
|
# num2bool.rb
|
|
|
|
#
|
|
|
|
|
|
|
|
module Puppet::Parser::Functions
|
|
|
|
newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS
|
2013-03-29 17:03:33 +01:00
|
|
|
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.
|
2011-04-25 04:47:31 +02:00
|
|
|
EOS
|
|
|
|
) do |arguments|
|
|
|
|
|
|
|
|
raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " +
|
2013-03-29 17:03:33 +01:00
|
|
|
"given (#{arguments.size} for 1)") if arguments.size != 1
|
2011-04-25 04:47:31 +02:00
|
|
|
|
2013-03-29 20:06:36 +01:00
|
|
|
number = arguments[0]
|
|
|
|
|
|
|
|
case number
|
|
|
|
when Numeric
|
|
|
|
# Yay, it's a number
|
|
|
|
when String
|
2013-04-01 17:44:09 +02:00
|
|
|
begin
|
|
|
|
number = Float(number)
|
|
|
|
rescue ArgumentError => ex
|
|
|
|
raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{ex.message}")
|
|
|
|
end
|
2013-03-29 20:06:36 +01:00
|
|
|
else
|
|
|
|
begin
|
|
|
|
number = number.to_s
|
2013-04-01 17:44:09 +02:00
|
|
|
rescue NoMethodError => ex
|
|
|
|
raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{ex.message}")
|
2013-03-29 20:06:36 +01:00
|
|
|
end
|
2011-04-25 04:47:31 +02:00
|
|
|
end
|
|
|
|
|
2013-04-01 17:44:09 +02:00
|
|
|
# Truncate Floats
|
2013-03-29 20:06:36 +01:00
|
|
|
number = number.to_i
|
|
|
|
|
2013-03-29 17:03:33 +01:00
|
|
|
# Return true for any positive number and false otherwise
|
2013-04-01 17:44:09 +02:00
|
|
|
return number > 0
|
2011-04-25 04:47:31 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 et :
|