summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions/num2bool.rb
blob: 2baef62778389141e13e7613fcc92986ac89334d (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
#
# num2bool.rb
#

# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ...

module Puppet::Parser::Functions
  newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS
    EOS
  ) do |arguments|

    raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " +
      "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')
    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

    return result
  end
end

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