Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / type3x.rb
1 #
2 # type3x.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:type3x, :type => :rvalue, :doc => <<-DOC
6     @summary
7       **DEPRECATED:** This function will be removed when Puppet 3 support is dropped; please migrate to the new parser's typing system.
8
9     @return the type when passed a value. Type can be one of:
10
11     * string
12     * array
13     * hash
14     * float
15     * integer
16     * boolean
17   DOC
18              ) do |args|
19     raise(Puppet::ParseError, "type3x(): Wrong number of arguments given (#{args.size} for 1)") unless args.size == 1
20
21     value = args[0]
22
23     klass = value.class
24
25     unless [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass) # rubocop:disable Lint/UnifiedInteger
26       raise(Puppet::ParseError, 'type3x(): 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 %r{^(?:Big|Fix)num$} then 'integer'
34              when %r{^(?:True|False)Class$} then 'boolean'
35              else klass
36              end
37
38     if result == 'String'
39       if value == value.to_i.to_s
40         result = 'Integer'
41       elsif value == value.to_f.to_s
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 :