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