comparison modules/stdlib/lib/puppet/parser/functions/size.rb @ 0:956e484adc12

Initial public release of Puppet configs
author IBBoard <dev@ibboard.co.uk>
date Sat, 16 Aug 2014 19:47:38 +0000
parents
children c42fb28cff86
comparison
equal deleted inserted replaced
-1:000000000000 0:956e484adc12
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 :