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