diff options
author | Travis Fields <travis@puppetlabs.com> | 2015-02-25 11:39:27 -0800 |
---|---|---|
committer | Travis Fields <travis@puppetlabs.com> | 2015-02-25 11:39:27 -0800 |
commit | 7021b1f55cdc320c7eb389cd91f6be294629669b (patch) | |
tree | 80734f517b7366c0eb22089384c0a727bccab944 | |
parent | 3da8d17390832daa7036df06976c442c4f2c7b5d (diff) | |
download | puppet-stdlib-7021b1f55cdc320c7eb389cd91f6be294629669b.tar.gz puppet-stdlib-7021b1f55cdc320c7eb389cd91f6be294629669b.tar.bz2 |
Add Hash to upcase
-rw-r--r-- | lib/puppet/parser/functions/upcase.rb | 15 | ||||
-rwxr-xr-x | spec/functions/upcase_spec.rb | 8 |
2 files changed, 17 insertions, 6 deletions
diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb index 4302b29..22eae3a 100644 --- a/lib/puppet/parser/functions/upcase.rb +++ b/lib/puppet/parser/functions/upcase.rb @@ -13,22 +13,27 @@ Converts a string or an array of strings to uppercase. Will return: ASDF - EOS + EOS ) do |arguments| raise(Puppet::ParseError, "upcase(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'upcase(): Requires either ' + - 'array or string to work with') + unless value.is_a?(Array) || value.is_a?(String) || value.is_a?(Hash) + raise(Puppet::ParseError, 'upcase(): Requires an ' + + 'array, string or hash to work with') end if value.is_a?(Array) # Numbers in Puppet are often string-encoded which is troublesome ... result = value.collect { |i| i.is_a?(String) ? i.upcase : i } + elsif value.is_a?(Hash) + result = {} + result << value.each_pair do |k, v| + return {k.upcase => v.collect! { |p| p.upcase }} + end else result = value.upcase end diff --git a/spec/functions/upcase_spec.rb b/spec/functions/upcase_spec.rb index 3cf8b05..a50a3ab 100755 --- a/spec/functions/upcase_spec.rb +++ b/spec/functions/upcase_spec.rb @@ -9,7 +9,7 @@ describe "the upcase function" do end it "should raise a ParseError if there is less than 1 arguments" do - expect { scope.function_upcase([]) }.to( raise_error(Puppet::ParseError)) + expect { scope.function_upcase([]) }.to(raise_error(Puppet::ParseError)) end it "should upcase a string" do @@ -30,4 +30,10 @@ describe "the upcase function" do result = scope.function_upcase([value]) result.should(eq('ABC')) end + + it 'should accept hashes and return uppercase' do + expect( + scope.function_upcase([{'test' => %w(this that and other thing)}]) + ).to eq({'TEST' => %w(THIS THAT AND OTHER THING)}) + end end |