Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / chomp.rb
1 #
2 #  chomp.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:chomp, :type => :rvalue, :doc => <<-'EOS'
7     Removes the record separator from the end of a string or an array of
8     strings, for example `hello\n` becomes `hello`.
9     Requires a single string or array as an input.
10     EOS
11   ) do |arguments|
12
13     raise(Puppet::ParseError, "chomp(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1
14
15     value = arguments[0]
16
17     unless value.is_a?(Array) || value.is_a?(String)
18       raise(Puppet::ParseError, 'chomp(): Requires either array or string to work with')
19     end
20
21     if value.is_a?(Array)
22       # Numbers in Puppet are often string-encoded which is troublesome ...
23       result = value.collect { |i| i.is_a?(String) ? i.chomp : i }
24     else
25       result = value.chomp
26     end
27
28     return result
29   end
30 end
31
32 # vim: set ts=2 sw=2 et :