From d38e399b47390f5bf0e7a5ff2669ab7e4f2fef3c Mon Sep 17 00:00:00 2001
From: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Date: Sat, 30 Apr 2011 14:35:10 +0100
Subject: First version.  Simple swapcase function to use within Puppet DSL.

Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
---
 swapcase.rb | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 swapcase.rb

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 :
-- 
cgit v1.2.3