2011-04-29 19:10:04 +02:00
|
|
|
#
|
|
|
|
# chomp.rb
|
|
|
|
#
|
|
|
|
|
|
|
|
module Puppet::Parser::Functions
|
2011-07-29 20:22:30 +02:00
|
|
|
newfunction(:chomp, :type => :rvalue, :doc => <<-'EOS'
|
2012-11-09 18:08:24 +01:00
|
|
|
Removes the record separator from the end of a string or an array of
|
2011-07-29 20:22:30 +02:00
|
|
|
strings, for example `hello\n` becomes `hello`.
|
|
|
|
Requires a single string or array as an input.
|
2011-04-29 19:10:04 +02:00
|
|
|
EOS
|
|
|
|
) do |arguments|
|
|
|
|
|
|
|
|
raise(Puppet::ParseError, "chomp(): Wrong number of arguments " +
|
|
|
|
"given (#{arguments.size} for 1)") if arguments.size < 1
|
|
|
|
|
|
|
|
value = arguments[0]
|
|
|
|
|
2014-04-22 09:36:28 +02:00
|
|
|
unless value.is_a?(Array) || value.is_a?(String)
|
2011-04-30 00:30:32 +02:00
|
|
|
raise(Puppet::ParseError, 'chomp(): Requires either ' +
|
2011-04-29 19:10:04 +02:00
|
|
|
'array or string to work with')
|
|
|
|
end
|
|
|
|
|
|
|
|
if value.is_a?(Array)
|
2011-04-30 03:54:47 +02:00
|
|
|
# Numbers in Puppet are often string-encoded which is troublesome ...
|
2011-04-29 19:10:04 +02:00
|
|
|
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 :
|