From aafce9c99b779855e1e10e5a337848fa9f676a01 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Sat, 30 Apr 2011 16:00:49 +0200 Subject: Moved more functions into lib/puppet/parser/functions/ --- lib/puppet/parser/functions/capitalize.rb | 32 +++++++++++++ lib/puppet/parser/functions/chomp.rb | 32 +++++++++++++ lib/puppet/parser/functions/chop.rb | 32 +++++++++++++ lib/puppet/parser/functions/date.rb | 12 +++++ lib/puppet/parser/functions/delete.rb | 15 ++++++ lib/puppet/parser/functions/downcase.rb | 32 +++++++++++++ lib/puppet/parser/functions/flatten.rb | 25 ++++++++++ lib/puppet/parser/functions/grep.rb | 12 +++++ lib/puppet/parser/functions/hash.rb | 34 +++++++++++++ lib/puppet/parser/functions/is_float.rb | 12 +++++ lib/puppet/parser/functions/is_integer.rb | 12 +++++ lib/puppet/parser/functions/is_numeric.rb | 12 +++++ .../parser/functions/is_valid_domain_name.rb | 12 +++++ lib/puppet/parser/functions/is_valid_ip_address.rb | 12 +++++ .../parser/functions/is_valid_mac_address.rb | 12 +++++ lib/puppet/parser/functions/is_valid_netmask.rb | 12 +++++ lib/puppet/parser/functions/load_json.rb | 12 +++++ lib/puppet/parser/functions/load_yaml.rb | 12 +++++ lib/puppet/parser/functions/lstrip.rb | 32 +++++++++++++ lib/puppet/parser/functions/member.rb | 14 +++--- lib/puppet/parser/functions/rand.rb | 12 +++++ lib/puppet/parser/functions/rstrip.rb | 31 ++++++++++++ lib/puppet/parser/functions/sort.rb | 12 +++++ lib/puppet/parser/functions/squeeze.rb | 12 +++++ lib/puppet/parser/functions/str2bool.rb | 38 +++++++++++++++ lib/puppet/parser/functions/strip.rb | 31 ++++++++++++ lib/puppet/parser/functions/swapcase.rb | 32 +++++++++++++ lib/puppet/parser/functions/upcase.rb | 32 +++++++++++++ lib/puppet/parser/functions/zip.rb | 56 ++++++++++++++++++++++ 29 files changed, 629 insertions(+), 7 deletions(-) create mode 100644 lib/puppet/parser/functions/capitalize.rb create mode 100644 lib/puppet/parser/functions/chomp.rb create mode 100644 lib/puppet/parser/functions/chop.rb create mode 100644 lib/puppet/parser/functions/date.rb create mode 100644 lib/puppet/parser/functions/delete.rb create mode 100644 lib/puppet/parser/functions/downcase.rb create mode 100644 lib/puppet/parser/functions/flatten.rb create mode 100644 lib/puppet/parser/functions/grep.rb create mode 100644 lib/puppet/parser/functions/hash.rb create mode 100644 lib/puppet/parser/functions/is_float.rb create mode 100644 lib/puppet/parser/functions/is_integer.rb create mode 100644 lib/puppet/parser/functions/is_numeric.rb create mode 100644 lib/puppet/parser/functions/is_valid_domain_name.rb create mode 100644 lib/puppet/parser/functions/is_valid_ip_address.rb create mode 100644 lib/puppet/parser/functions/is_valid_mac_address.rb create mode 100644 lib/puppet/parser/functions/is_valid_netmask.rb create mode 100644 lib/puppet/parser/functions/load_json.rb create mode 100644 lib/puppet/parser/functions/load_yaml.rb create mode 100644 lib/puppet/parser/functions/lstrip.rb create mode 100644 lib/puppet/parser/functions/rand.rb create mode 100644 lib/puppet/parser/functions/rstrip.rb create mode 100644 lib/puppet/parser/functions/sort.rb create mode 100644 lib/puppet/parser/functions/squeeze.rb create mode 100644 lib/puppet/parser/functions/str2bool.rb create mode 100644 lib/puppet/parser/functions/strip.rb create mode 100644 lib/puppet/parser/functions/swapcase.rb create mode 100644 lib/puppet/parser/functions/upcase.rb create mode 100644 lib/puppet/parser/functions/zip.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/capitalize.rb b/lib/puppet/parser/functions/capitalize.rb new file mode 100644 index 0000000..f902cb3 --- /dev/null +++ b/lib/puppet/parser/functions/capitalize.rb @@ -0,0 +1,32 @@ +# +# capitalize.rb +# + +module Puppet::Parser::Functions + newfunction(:capitalize, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "capitalize(): 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, 'capitalize(): 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.capitalize : i } + else + result = value.capitalize + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/chomp.rb b/lib/puppet/parser/functions/chomp.rb new file mode 100644 index 0000000..e1d788a --- /dev/null +++ b/lib/puppet/parser/functions/chomp.rb @@ -0,0 +1,32 @@ +# +# chomp.rb +# + +module Puppet::Parser::Functions + newfunction(:chomp, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "chomp(): 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, 'chomp(): 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.chomp : i } + else + result = value.chomp + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/chop.rb b/lib/puppet/parser/functions/chop.rb new file mode 100644 index 0000000..0f9af86 --- /dev/null +++ b/lib/puppet/parser/functions/chop.rb @@ -0,0 +1,32 @@ +# +# chop.rb +# + +module Puppet::Parser::Functions + newfunction(:chop, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "chop(): 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, 'chop(): Requires either an ' + + '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.chop : i } + else + result = value.chop + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb new file mode 100644 index 0000000..ea11265 --- /dev/null +++ b/lib/puppet/parser/functions/date.rb @@ -0,0 +1,12 @@ +# +# date.rb +# + +module Puppet::Parser::Functions + newfunction(:date, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb new file mode 100644 index 0000000..b8376d1 --- /dev/null +++ b/lib/puppet/parser/functions/delete.rb @@ -0,0 +1,15 @@ +# +# delete.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... + +module Puppet::Parser::Functions + newfunction(:delete, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/downcase.rb b/lib/puppet/parser/functions/downcase.rb new file mode 100644 index 0000000..71f8480 --- /dev/null +++ b/lib/puppet/parser/functions/downcase.rb @@ -0,0 +1,32 @@ +# +# downcase.rb +# + +module Puppet::Parser::Functions + newfunction(:downcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "downcase(): 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, 'downcase(): 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.downcase : i } + else + result = value.downcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/flatten.rb b/lib/puppet/parser/functions/flatten.rb new file mode 100644 index 0000000..6036f72 --- /dev/null +++ b/lib/puppet/parser/functions/flatten.rb @@ -0,0 +1,25 @@ +# +# flatten.rb +# + +module Puppet::Parser::Functions + newfunction(:flatten, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'flatten(): Requires array to work with') + end + + result = array.flatten + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb new file mode 100644 index 0000000..1663fe7 --- /dev/null +++ b/lib/puppet/parser/functions/grep.rb @@ -0,0 +1,12 @@ +# +# grep.rb +# + +module Puppet::Parser::Functions + newfunction(:grep, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/hash.rb b/lib/puppet/parser/functions/hash.rb new file mode 100644 index 0000000..f0c01d4 --- /dev/null +++ b/lib/puppet/parser/functions/hash.rb @@ -0,0 +1,34 @@ +# +# hash.rb +# + +module Puppet::Parser::Functions + newfunction(:hash, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "hash(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'hash(): Requires array to work with') + end + + result = {} + + begin + # This is to make it compatible with older version of Ruby ... + array = array.flatten + result = Hash[*array] + rescue Exception + raise(Puppet::ParseError, 'hash(): Unable to compute ' + + 'hash from array given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb new file mode 100644 index 0000000..2a5a923 --- /dev/null +++ b/lib/puppet/parser/functions/is_float.rb @@ -0,0 +1,12 @@ +# +# is_float.rb +# + +module Puppet::Parser::Functions + newfunction(:is_float, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb new file mode 100644 index 0000000..44337f0 --- /dev/null +++ b/lib/puppet/parser/functions/is_integer.rb @@ -0,0 +1,12 @@ +# +# is_integer.rb +# + +module Puppet::Parser::Functions + newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb new file mode 100644 index 0000000..7a64091 --- /dev/null +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -0,0 +1,12 @@ +# +# is_numeric.rb +# + +module Puppet::Parser::Functions + newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb new file mode 100644 index 0000000..05d64be --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_domain_name.rb @@ -0,0 +1,12 @@ +# +# is_valid_doman_name.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_doman_name, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb new file mode 100644 index 0000000..e6b68a8 --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_ip_address.rb @@ -0,0 +1,12 @@ +# +# is_valid_ip_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb new file mode 100644 index 0000000..5e354c9 --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_mac_address.rb @@ -0,0 +1,12 @@ +# +# is_valid_mac_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_netmask.rb b/lib/puppet/parser/functions/is_valid_netmask.rb new file mode 100644 index 0000000..2aeb374 --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_netmask.rb @@ -0,0 +1,12 @@ +# +# is_valid_netmask.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_netmask, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_json.rb b/lib/puppet/parser/functions/load_json.rb new file mode 100644 index 0000000..9ec8c78 --- /dev/null +++ b/lib/puppet/parser/functions/load_json.rb @@ -0,0 +1,12 @@ +# +# load_json.rb +# + +module Puppet::Parser::Functions + newfunction(:load_json, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_yaml.rb b/lib/puppet/parser/functions/load_yaml.rb new file mode 100644 index 0000000..684a721 --- /dev/null +++ b/lib/puppet/parser/functions/load_yaml.rb @@ -0,0 +1,12 @@ +# +# load_yaml.rb +# + +module Puppet::Parser::Functions + newfunction(:load_yaml, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/lstrip.rb b/lib/puppet/parser/functions/lstrip.rb new file mode 100644 index 0000000..a7b2656 --- /dev/null +++ b/lib/puppet/parser/functions/lstrip.rb @@ -0,0 +1,32 @@ +# +# lstrip.rb +# + +module Puppet::Parser::Functions + newfunction(:lstrip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "lstrip(): 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, 'lstrip(): 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.lstrip : i } + else + result = value.lstrip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index a491a76..bb43a37 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -1,28 +1,28 @@ # -# include.rb +# member.rb # # TODO(Krzysztof Wilczynski): We need to add support for regular expression ... # TODO(Krzysztof Wilczynski): Support for strings and hashes too ... module Puppet::Parser::Functions - newfunction(:includes, :type => :rvalue, :doc => <<-EOS + newfunction(:member, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| - raise(Puppet::ParseError, "includes(): Wrong number of arguments " + + raise(Puppet::ParseError, "member(): Wrong number of arguments " + "given (#{arguments.size} for 2)") if arguments.size < 2 array = arguments[0] - if not array.is_a?(Array) - raise(Puppet::ParseError, 'includes(): Requires an array to work with') + unless array.is_a?(Array) + raise(Puppet::ParseError, 'member(): Requires array to work with') end item = arguments[1] - raise(Puppet::ParseError, 'includes(): You must provide item ' + - 'to search for within given array') if item.empty? + raise(Puppet::ParseError, 'member(): You must provide item ' + + 'to search for within array given') if item.empty? result = array.include?(item) diff --git a/lib/puppet/parser/functions/rand.rb b/lib/puppet/parser/functions/rand.rb new file mode 100644 index 0000000..2cb9acb --- /dev/null +++ b/lib/puppet/parser/functions/rand.rb @@ -0,0 +1,12 @@ +# +# rand.rb +# + +module Puppet::Parser::Functions + newfunction(:rand, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/rstrip.rb b/lib/puppet/parser/functions/rstrip.rb new file mode 100644 index 0000000..56849d3 --- /dev/null +++ b/lib/puppet/parser/functions/rstrip.rb @@ -0,0 +1,31 @@ +# +# rstrip.rb +# + +module Puppet::Parser::Functions + newfunction(:rstrip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "rstrip(): 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, 'rstrip(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.rstrip : i } + else + result = value.rstrip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb new file mode 100644 index 0000000..85e5ba0 --- /dev/null +++ b/lib/puppet/parser/functions/sort.rb @@ -0,0 +1,12 @@ +# +# sort.rb +# + +module Puppet::Parser::Functions + newfunction(:sort, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb new file mode 100644 index 0000000..a135bd3 --- /dev/null +++ b/lib/puppet/parser/functions/squeeze.rb @@ -0,0 +1,12 @@ +# +# squeeze.rb +# + +module Puppet::Parser::Functions + newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb new file mode 100644 index 0000000..f3a6d6c --- /dev/null +++ b/lib/puppet/parser/functions/str2bool.rb @@ -0,0 +1,38 @@ +# +# str2bool.rb +# + +module Puppet::Parser::Functions + newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "str2bool(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + string = arguments[0] + + unless string.is_a?(String) + raise(Puppet::ParseError, 'str2bool(): Requires either ' + + 'string to work with') + end + + # We consider all the yes, no, y, n and so on too ... + result = case string + # + # This is how undef looks like in Puppet ... + # We yield false in this case. + # + when /^$/, '' then false # Empty string will be false ... + when /^(1|t|y|true|yes)$/ then true + when /^(0|f|n|false|no)$/ then false + when /^(undef|undefined)$/ then false # This is not likely to happen ... + else + raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/strip.rb b/lib/puppet/parser/functions/strip.rb new file mode 100644 index 0000000..ebab20e --- /dev/null +++ b/lib/puppet/parser/functions/strip.rb @@ -0,0 +1,31 @@ +# +# strip.rb +# + +module Puppet::Parser::Functions + newfunction(:strip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "strip(): 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, 'strip(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.strip : i } + else + result = value.strip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb new file mode 100644 index 0000000..a0002a9 --- /dev/null +++ b/lib/puppet/parser/functions/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 : diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb new file mode 100644 index 0000000..8a9769d --- /dev/null +++ b/lib/puppet/parser/functions/upcase.rb @@ -0,0 +1,32 @@ +# +# upcase.rb +# + +module Puppet::Parser::Functions + newfunction(:upcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "upcase(): 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, 'upcase(): 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.upcase : i } + else + result = value.upcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb new file mode 100644 index 0000000..024d64a --- /dev/null +++ b/lib/puppet/parser/functions/zip.rb @@ -0,0 +1,56 @@ +# +# zip.rb +# + +module Puppet::Parser::Functions + newfunction(:zip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support three arguments but only first is mandatory ... + raise(Puppet::ParseError, "zip(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + a = arguments[0] + b = arguments[1] + + unless a.is_a?(Array) and b.is_a?(Array) + raise(Puppet::ParseError, 'zip(): Requires array to work with') + end + + flatten = arguments[2] if arguments[2] + + if flatten + klass = flatten.class + + # We can have either true or false, or string which resembles boolean ... + unless [FalseClass, TrueClass, String].include?(klass) + raise(Puppet::ParseError, 'zip(): Requires either ' + + 'boolean or string to work with') + end + + if flatten.is_a?(String) + # We consider all the yes, no, y, n and so on too ... + flatten = case flatten + # + # This is how undef looks like in Puppet ... + # We yield false in this case. + # + when /^$/, '' then false # Empty string will be false ... + when /^(1|t|y|true|yes)$/ then true + when /^(0|f|n|false|no)$/ then false + when /^(undef|undefined)$/ then false # This is not likely to happen ... + else + raise(Puppet::ParseError, 'zip(): Unknown type of boolean given') + end + end + end + + result = a.zip(b) + result = flatten ? result.flatten : result + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3