summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions/num2bool.rb
blob: cf98f805c14c9ba600e144ee389a9a78ecabee2a (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#
# 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

    number = arguments[0]

    case number
    when Numeric
      # Yay, it's a number
    when String
      # Deal with strings later
    else
      begin
        number = number.to_s
      rescue NoMethodError
        raise(Puppet::ParseError, 'num2bool(): Unable to parse argument: ' + $!)
      end
    end

    case number
    when String
      # Only accept strings that look somewhat like numbers
      unless number =~ /^-?\d+/
        raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number")
      end
    end

    # Truncate floats
    number = number.to_i

    # Return true for any positive number and false otherwise
    return number > 0 ? true : false
  end
end

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