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