Skip to content

Commit 6d07a6a

Browse files
author
Morgan Haskel
committed
Merge pull request #419 from cyberious/master
Loosen the restrictions of upcase and allow for recursion of the objects...
2 parents cd65680 + 85e81f9 commit 6d07a6a

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ Takes a single string value as an argument. *Type*: rvalue
475475

476476
You can also use this with arrays. For example, `unique(["a","a","b","b","c","c"])` returns ["a","b","c"]. *Type*: rvalue
477477

478-
* `upcase`: Converts a string or an array of strings to uppercase. For example, `upcase("abcd")` returns 'ABCD'. *Type*: rvalue
478+
* `upcase`: Converts an object, array or hash of objects that respond to upcase to uppercase. For example, `upcase("abcd")` returns 'ABCD'. *Type*: rvalue
479479

480480
* `uriescape`: Urlencodes a string or array of strings. Requires either a single string or an array as an input. *Type*: rvalue
481481

lib/puppet/parser/functions/upcase.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ module Puppet::Parser::Functions
1717
) do |arguments|
1818

1919
raise(Puppet::ParseError, "upcase(): Wrong number of arguments " +
20-
"given (#{arguments.size} for 1)") if arguments.size < 1
20+
"given (#{arguments.size} for 1)") if arguments.size != 1
2121

2222
value = arguments[0]
2323

24-
unless value.is_a?(Array) || value.is_a?(String) || value.is_a?(Hash)
24+
unless value.is_a?(Array) || value.is_a?(Hash) || value.respond_to?(:upcase)
2525
raise(Puppet::ParseError, 'upcase(): Requires an ' +
26-
'array, string or hash to work with')
26+
'array, hash or object that responds to upcase in order to work')
2727
end
2828

2929
if value.is_a?(Array)
3030
# Numbers in Puppet are often string-encoded which is troublesome ...
31-
result = value.collect { |i| i.is_a?(String) ? i.upcase : i }
31+
result = value.collect { |i| function_upcase([i]) }
3232
elsif value.is_a?(Hash)
3333
result = {}
3434
value.each_pair do |k, v|
35-
result.merge!({k.upcase => v.collect! { |p| p.upcase }})
35+
result[function_upcase([k])] = function_upcase([v])
3636
end
3737
else
3838
result = value.upcase

spec/functions/upcase_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,23 @@ class AlsoString < String
3636
scope.function_upcase([{'test' => %w(this that and other thing)}])
3737
).to eq({'TEST' => %w(THIS THAT AND OTHER THING)})
3838
end
39+
40+
if :test.respond_to?(:upcase)
41+
it 'should accept hashes of symbols' do
42+
expect(
43+
scope.function_upcase([{:test => [:this, :that, :other]}])
44+
).to eq({:TEST => [:THIS, :THAT, :OTHER]})
45+
end
46+
it 'should return upcase symbol' do
47+
expect(
48+
scope.function_upcase([:test])
49+
).to eq(:TEST)
50+
end
51+
it 'should return mixed objects in upcease' do
52+
expect(
53+
scope.function_upcase([[:test, 'woot']])
54+
).to eq([:TEST, 'WOOT'])
55+
56+
end
57+
end
3958
end

0 commit comments

Comments
 (0)