From 88a93ac6cdf38045e1cf29325a70e5e4143016b3 Mon Sep 17 00:00:00 2001 From: Richard Soderberg Date: Tue, 26 Mar 2013 15:45:40 -0700 Subject: add suffix function to accompany the prefix function --- lib/puppet/parser/functions/suffix.rb | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/puppet/parser/functions/suffix.rb (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb new file mode 100644 index 0000000..5018280 --- /dev/null +++ b/lib/puppet/parser/functions/suffix.rb @@ -0,0 +1,45 @@ +# +# suffix.rb +# + +module Puppet::Parser::Functions + newfunction(:suffix, :type => :rvalue, :doc => <<-EOS +This function applies a suffix to all elements in an array. + +*Examples:* + + suffix(['a','b','c'], 'p') + +Will return: ['ap','bp','cp'] + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "suffix(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'suffix(): Requires array to work with') + end + + suffix = arguments[1] if arguments[1] + + if suffix + unless suffix.is_a?(String) + raise(Puppet::ParseError, 'suffix(): Requires string to work with') + end + end + + # Turn everything into string same as join would do ... + result = array.collect do |i| + i = i.to_s + suffix ? i + suffix : i + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From a83318d3ee41683118a55a2b15769c97efbcf6d9 Mon Sep 17 00:00:00 2001 From: Richard Soderberg Date: Tue, 26 Mar 2013 15:49:09 -0700 Subject: prefix: fix doc typo Examles -> Examples --- lib/puppet/parser/functions/prefix.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 4593976..243cf18 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -6,7 +6,7 @@ module Puppet::Parser::Functions newfunction(:prefix, :type => :rvalue, :doc => <<-EOS This function applies a prefix to all elements in an array. -*Examles:* +*Examples:* prefix(['a','b','c'], 'p') -- cgit v1.2.3 From 29402f31e73a426dcc449216010834884d0251ec Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Wed, 27 Mar 2013 13:37:25 -0700 Subject: (maint) better error reporting for prefix and suffix When prefix and suffix did error checking with positional arguments, they would not report the position of the argument that failed to validate. This commit changes the messages to indicate which argument failed. --- lib/puppet/parser/functions/prefix.rb | 4 ++-- lib/puppet/parser/functions/suffix.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 243cf18..62211ae 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -21,14 +21,14 @@ Will return: ['pa','pb','pc'] array = arguments[0] unless array.is_a?(Array) - raise(Puppet::ParseError, 'prefix(): Requires array to work with') + raise Puppet::ParseError, "prefix(): expected first argument to be an Array, got #{array.inspect}" end prefix = arguments[1] if arguments[1] if prefix unless prefix.is_a?(String) - raise(Puppet::ParseError, 'prefix(): Requires string to work with') + raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{suffix.inspect}" end end diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb index 5018280..f7792d6 100644 --- a/lib/puppet/parser/functions/suffix.rb +++ b/lib/puppet/parser/functions/suffix.rb @@ -21,14 +21,14 @@ Will return: ['ap','bp','cp'] array = arguments[0] unless array.is_a?(Array) - raise(Puppet::ParseError, 'suffix(): Requires array to work with') + raise Puppet::ParseError, "suffix(): expected first argument to be an Array, got #{array.inspect}" end suffix = arguments[1] if arguments[1] if suffix - unless suffix.is_a?(String) - raise(Puppet::ParseError, 'suffix(): Requires string to work with') + unless suffix.is_a? String + raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}" end end -- cgit v1.2.3