summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Hulihan <hulihan.tom159@gmail.com>2014-06-27 11:45:42 -0400
committerTom Hulihan <hulihan.tom159@gmail.com>2014-06-27 12:38:22 -0400
commit3ef3766263eeee8f5f0d136273e08189f3ba8b05 (patch)
tree96313bcc2e8e4130aee5adba4e9aba3854ac8412
parent59b0c0e009cffb8c67eb59cdcd72413092e1bea2 (diff)
downloadhashie-3ef3766263eeee8f5f0d136273e08189f3ba8b05.tar.gz
Fix Hashie::Mash#values_at.
This method now converts the keys before trying to access them from the Mash.
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/hashie/mash.rb4
-rw-r--r--spec/hashie/mash_spec.rb23
3 files changed, 28 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c7d5b75..e46b4e9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,6 @@
## Next
+* [#179](https://github.com/intridea/hashie/pull/179) Mash#values_at will convert each key before doing the lookup - [@nahiluhmot](https://github.com/nahiluhmot).
* Your contribution here.
## 3.1 (6/25/2014)
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index 9991fb1..78f63e3 100644
--- a/lib/hashie/mash.rb
+++ b/lib/hashie/mash.rb
@@ -128,6 +128,10 @@ module Hashie
super(convert_key(key))
end
+ def values_at(*keys)
+ super(*keys.map { |key| convert_key(key) })
+ end
+
alias_method :regular_dup, :dup
# Duplicates the current mash as a new mash.
def dup
diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb
index f7a3d19..9925e93 100644
--- a/spec/hashie/mash_spec.rb
+++ b/spec/hashie/mash_spec.rb
@@ -476,4 +476,27 @@ describe Hashie::Mash do
expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => { '345' => 'hey' }]
end
end
+
+ describe '#values_at' do
+ let(:hash) { { 'key_one' => 1, :key_two => 2 } }
+ let(:mash) { Hashie::Mash.new(hash) }
+
+ context 'when the original type is given' do
+ it 'returns the values' do
+ expect(mash.values_at('key_one', :key_two)).to eq([1, 2])
+ end
+ end
+
+ context 'when a different, but acceptable type is given' do
+ it 'returns the values' do
+ expect(mash.values_at(:key_one, 'key_two')).to eq([1, 2])
+ end
+ end
+
+ context 'when a key is given that is not in the Mash' do
+ it 'returns nil for that value' do
+ expect(mash.values_at('key_one', :key_three)).to eq([1, nil])
+ end
+ end
+ end
end