Skip to content

Commit 1ccc4f5

Browse files
author
Morgan Haskel
committed
Merge pull request #417 from cyberious/UpcaseHash
Add Hash to upcase
2 parents 3da8d17 + 7021b1f commit 1ccc4f5

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

lib/puppet/parser/functions/upcase.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,27 @@ module Puppet::Parser::Functions
1313
Will return:
1414
1515
ASDF
16-
EOS
16+
EOS
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)
25-
raise(Puppet::ParseError, 'upcase(): Requires either ' +
26-
'array or string to work with')
24+
unless value.is_a?(Array) || value.is_a?(String) || value.is_a?(Hash)
25+
raise(Puppet::ParseError, 'upcase(): Requires an ' +
26+
'array, string or hash to work with')
2727
end
2828

2929
if value.is_a?(Array)
3030
# Numbers in Puppet are often string-encoded which is troublesome ...
3131
result = value.collect { |i| i.is_a?(String) ? i.upcase : i }
32+
elsif value.is_a?(Hash)
33+
result = {}
34+
result << value.each_pair do |k, v|
35+
return {k.upcase => v.collect! { |p| p.upcase }}
36+
end
3237
else
3338
result = value.upcase
3439
end

spec/functions/upcase_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
end
1010

1111
it "should raise a ParseError if there is less than 1 arguments" do
12-
expect { scope.function_upcase([]) }.to( raise_error(Puppet::ParseError))
12+
expect { scope.function_upcase([]) }.to(raise_error(Puppet::ParseError))
1313
end
1414

1515
it "should upcase a string" do
@@ -30,4 +30,10 @@ class AlsoString < String
3030
result = scope.function_upcase([value])
3131
result.should(eq('ABC'))
3232
end
33+
34+
it 'should accept hashes and return uppercase' do
35+
expect(
36+
scope.function_upcase([{'test' => %w(this that and other thing)}])
37+
).to eq({'TEST' => %w(THIS THAT AND OTHER THING)})
38+
end
3339
end

0 commit comments

Comments
 (0)