summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJerry Cheung <jollyjerry@gmail.com>2013-04-23 19:43:38 -0700
committerJerry Cheung <jollyjerry@gmail.com>2013-04-23 19:43:38 -0700
commit1194ddd525d612a776db413f4ef870f599cda423 (patch)
treed5d4ff473378d53a6cf44166739c56e8a743c5e3
parente985320b844c7670bfd9c3b001305b7276c9785a (diff)
parent5cd4f8a5598d28fb6083e0e6f25216e0b6db639a (diff)
downloadhashie-1194ddd525d612a776db413f4ef870f599cda423.tar.gz
Merge pull request #94 from markiz/issue-92-93
Make Mash#fetch behavior more consistent with Hash#fetch
-rw-r--r--lib/hashie/mash.rb4
-rw-r--r--spec/hashie/mash_spec.rb16
2 files changed, 14 insertions, 6 deletions
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index 29b6271..6bd1ca4 100644
--- a/lib/hashie/mash.rb
+++ b/lib/hashie/mash.rb
@@ -114,8 +114,8 @@ module Hashie
end
end
- def fetch(key, default_value = nil)
- self[key] || block_given? && yield(key) || default_value || super(key)
+ def fetch(key, *args)
+ super(convert_key(key), *args)
end
def delete(key)
diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb
index ef688ab..7842716 100644
--- a/spec/hashie/mash_spec.rb
+++ b/spec/hashie/mash_spec.rb
@@ -297,19 +297,19 @@ describe Hashie::Mash do
it 'should respond to a set key' do
Hashie::Mash.new(:abc => 'def').should be_respond_to(:abc)
end
-
+
it 'should respond to a set key with a suffix' do
%w(= ? ! _).each do |suffix|
Hashie::Mash.new(:abc => 'def').should be_respond_to(:"abc#{suffix}")
end
end
-
+
it 'should respond to an unknown key with a suffix' do
%w(= ? ! _).each do |suffix|
Hashie::Mash.new(:abc => 'def').should be_respond_to(:"xyz#{suffix}")
end
end
-
+
it "should not respond to an unknown key without a suffix" do
Hashie::Mash.new(:abc => 'def').should_not be_respond_to(:xyz)
end
@@ -365,7 +365,7 @@ describe Hashie::Mash do
end
describe "#fetch" do
- let(:hash) { {:one => 1} }
+ let(:hash) { {:one => 1, :other => false} }
let(:mash) { Hashie::Mash.new(hash) }
context "when key exists" do
@@ -373,6 +373,10 @@ describe Hashie::Mash do
mash.fetch(:one).should eql(1)
end
+ it "returns the value even if the value is falsy" do
+ mash.fetch(:other).should eql(false)
+ end
+
context "when key has other than original but acceptable type" do
it "returns the value" do
mash.fetch('one').should eql(1)
@@ -390,6 +394,10 @@ describe Hashie::Mash do
it "returns default value" do
mash.fetch(:two, 8).should eql(8)
end
+
+ it "returns default value even if it is falsy" do
+ mash.fetch(:two, false).should eql(false)
+ end
end
context "with block given" do