]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/lib/puppet/parser/functions/chomp.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / 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 " +
14       "given (#{arguments.size} for 1)") if arguments.size < 1
15
16     value = arguments[0]
17     klass = value.class
18
19     unless [Array, String].include?(klass)
20       raise(Puppet::ParseError, 'chomp(): Requires either ' +
21         'array or string to work with')
22     end
23
24     if value.is_a?(Array)
25       # Numbers in Puppet are often string-encoded which is troublesome ...
26       result = value.collect { |i| i.is_a?(String) ? i.chomp : i }
27     else
28       result = value.chomp
29     end
30
31     return result
32   end
33 end
34
35 # vim: set ts=2 sw=2 et :