]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/lib/puppet/parser/functions/type.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / modules / stdlib / lib / puppet / parser / functions / type.rb
1 #
2 # type.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:type, :type => :rvalue, :doc => <<-EOS
7 Returns the type when passed a variable. Type can be one of:
8
9 * string
10 * array
11 * hash
12 * float
13 * integer
14 * boolean
15     EOS
16   ) do |arguments|
17
18     raise(Puppet::ParseError, "type(): Wrong number of arguments " +
19       "given (#{arguments.size} for 1)") if arguments.size < 1
20
21     value = arguments[0]
22
23     klass = value.class
24
25     if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass)
26       raise(Puppet::ParseError, 'type(): Unknown type')
27     end
28
29     klass = klass.to_s # Ugly ...
30
31     # We note that Integer is the parent to Bignum and Fixnum ...
32     result = case klass
33       when /^(?:Big|Fix)num$/ then 'integer'
34       when /^(?:True|False)Class$/ then 'boolean'
35       else klass
36     end
37
38     if result == "String" then
39       if value == value.to_i.to_s then
40         result = "Integer"
41       elsif value == value.to_f.to_s then
42         result = "Float"
43       end
44     end
45
46     return result.downcase
47   end
48 end
49
50 # vim: set ts=2 sw=2 et :