diff options
author | Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> | 2011-04-26 01:29:53 +0100 |
---|---|---|
committer | Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> | 2011-04-26 01:29:53 +0100 |
commit | 528a16e3770b289c7bae315bf54d9ab07f8d6654 (patch) | |
tree | cd9f102a0efde4c78e698729ad9066a3f16268f4 | |
parent | c9dcca03b5106a69a39e88ec1c0c53a7ffc121e7 (diff) | |
download | puppet-stdlib-528a16e3770b289c7bae315bf54d9ab07f8d6654.tar.gz puppet-stdlib-528a16e3770b289c7bae315bf54d9ab07f8d6654.tar.bz2 |
First version. Simple size function to use within Puppet DSL.
Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
-rw-r--r-- | size.rb | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -0,0 +1,45 @@ +# +# size.rb +# + +module Puppet::Parser::Functions + newfunction(:size, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "size(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + item = arguments[0] + + if item.is_a?(String) + + begin + # + # Check whether your item is a numeric value or not ... + # This will take care about positive and/or negative numbers + # for both integer and floating-point values ... + # + # Please note that Puppet has no notion of hexadecimal + # nor octal numbers for at this point in time ... + # + Float(item) + + raise(Puppet::ParseError, 'size(): Requires either ' + + 'string or array to work with') + + rescue ArgumentError + result = item.size + end + + elsif item.is_a?(Array) + result = item.size + else + raise(Puppet::ParseError, 'size(): Unknown type given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : |