Skip to content

Commit

Permalink
Return nil for key that is not found instead of raising exception. Ex…
Browse files Browse the repository at this point in the history
…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 cadd288
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 8 deletions.
1 change: 0 additions & 1 deletion lib/mongo_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 0 additions & 3 deletions lib/mongo_mapper/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions lib/mongo_mapper/plugins/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_embedded_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit cadd288

Please sign in to comment.