Permalink
Browse files

Return nil for key that is not found instead of raising exception. Ex…

…ception is way to harsh. This makes it behave more like a hash.
  • Loading branch information...
jnunemaker committed Jun 24, 2010
1 parent d3fd2a5 commit cadd288fea8972b2e96dd46c0e0d61ab8c3957a4
View
@@ -8,7 +8,6 @@ module MongoMapper
autoload :Connection, 'mongo_mapper/connection'
autoload :Error, 'mongo_mapper/exceptions'
autoload :KeyNotFound, 'mongo_mapper/exceptions'
autoload :DocumentNotFound, 'mongo_mapper/exceptions'
autoload :InvalidScheme, 'mongo_mapper/exceptions'
autoload :DocumentNotValid, 'mongo_mapper/exceptions'
@@ -3,9 +3,6 @@ module MongoMapper
# generic MM error
class Error < StandardError; end
# raised when key expected to exist but not found
class KeyNotFound < Error; end
# raised when document expected but not found
class DocumentNotFound < Error; end
@@ -290,8 +290,6 @@ def read_key(key_name)
value = key.get(instance_variable_get(:"@#{key_name}"))
set_parent_document(key, value)
instance_variable_set(:"@#{key_name}", value)
else
raise KeyNotFound, "Could not find key: #{key_name.inspect}"
end
end
@@ -387,9 +387,9 @@ def passwd
doc[:name].should == 'string'
end
should "raise exception when key not found" do
should "return nil when not found" do
doc = @document.new(:name => 'string')
assert_raises(MongoMapper::KeyNotFound) { doc[:not_here] }
doc[:not_here].should be_nil
end
end

0 comments on commit cadd288

Please sign in to comment.