Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / size.rb
1 #
2 # size.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:size, :type => :rvalue, :doc => <<-DOC
6     @summary
7       Returns the number of elements in a string, an array or a hash
8
9     @return
10       the number of elements in a string, an array or a hash
11
12     > *Note:* that since Puppet 5.4.0, the length() function in Puppet is preferred over this. For versions
13     of Puppet < 5.4.0 use the stdlib length() function.
14   DOC
15              ) do |arguments|
16
17     raise(Puppet::ParseError, "size(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
18
19     item = arguments[0]
20
21     function_deprecation([:size, 'This method is going to be deprecated, please use the stdlib length function.'])
22
23     if item.is_a?(String)
24
25       begin
26         #
27         # Check whether your item is a numeric value or not ...
28         # This will take care about positive and/or negative numbers
29         # for both integer and floating-point values ...
30         #
31         # Please note that Puppet has no notion of hexadecimal
32         # nor octal numbers for its DSL at this point in time ...
33         #
34         Float(item)
35
36         raise(Puppet::ParseError, 'size(): Requires either string, array or hash to work with')
37       rescue ArgumentError
38         result = item.size
39       end
40
41     elsif item.is_a?(Array) || item.is_a?(Hash)
42       result = item.size
43     else
44       raise(Puppet::ParseError, 'size(): Unknown type given')
45     end
46
47     return result
48   end
49 end
50
51 # vim: set ts=2 sw=2 et :