summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions/num2bool.rb
blob: 15dd5601e51f0c4649386d8d06879c832ef1b5ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#
# num2bool.rb
#

module Puppet::Parser::Functions
  newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS
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

    # 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 true for any positive number and false otherwise
    return number > 0 ? true : false
  end
end

# vim: set ts=2 sw=2 et :