37
|
1 #
|
|
2 # camelcase.rb
|
272
|
3 # Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
|
37
|
4 #
|
|
5 module Puppet::Parser::Functions
|
272
|
6 newfunction(:camelcase, :type => :rvalue, :doc => <<-DOC
|
|
7 Converts the case of a string or all strings in an array to camel case.
|
|
8 DOC
|
|
9 ) do |arguments|
|
37
|
10
|
272
|
11 raise(Puppet::ParseError, "camelcase(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
|
37
|
12
|
|
13 value = arguments[0]
|
|
14 klass = value.class
|
|
15
|
|
16 unless [Array, String].include?(klass)
|
272
|
17 raise(Puppet::ParseError, 'camelcase(): Requires either array or string to work with')
|
37
|
18 end
|
|
19
|
272
|
20 result = if value.is_a?(Array)
|
|
21 # Numbers in Puppet are often string-encoded which is troublesome ...
|
|
22 value.map { |i| i.is_a?(String) ? i.split('_').map { |e| e.capitalize }.join : i }
|
|
23 else
|
|
24 value.split('_').map { |e| e.capitalize }.join
|
|
25 end
|
37
|
26
|
|
27 return result
|
|
28 end
|
|
29 end
|
|
30
|
|
31 # vim: set ts=2 sw=2 et :
|