diff options
author | Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> | 2011-04-30 14:35:10 +0100 |
---|---|---|
committer | Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> | 2011-04-30 14:35:10 +0100 |
commit | d38e399b47390f5bf0e7a5ff2669ab7e4f2fef3c (patch) | |
tree | be12cb7c37af3ad735ae0c2fdb65e4c6c82f176e /swapcase.rb | |
parent | 8733a57ffb6e831274e73eb03fe1662ffa6e04d7 (diff) |
First version. Simple swapcase function to use within Puppet DSL.
Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Diffstat (limited to 'swapcase.rb')
-rw-r--r-- | swapcase.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/swapcase.rb b/swapcase.rb new file mode 100644 index 0000000..a0002a9 --- /dev/null +++ b/swapcase.rb @@ -0,0 +1,32 @@ +# +# swapcase.rb +# + +module Puppet::Parser::Functions + newfunction(:swapcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "swapcase(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'swapcase(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.swapcase : i } + else + result = value.swapcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : |