After looking at the code, I have a suggestion that doesn't change the
behavior, but might be a technical improvement. It would involve small
changes to both mongo ruby driver and mongomapper...
I noticed that the mongo ruby driver does not use a named exception
class corresponding to 'illegal ObjectID format'. It just raises an
exception with that string. Might it be better to give it a name, e.g.
IllegalObjectIDFormat? This would allow MongoMapper to rescue that
exception specifically -- or pass it along if it wanted.
This is what the code looks like now in mongomapper-0.3.1/lib/
mongomapper.rb:
def self.mm_typecast(value)
begin
if value.is_a?(XGen::Mongo::Driver::ObjectID)
value
else
XGen::Mongo::Driver::ObjectID::from_string(value.to_s)
end
rescue => exception
if exception.message == 'illegal ObjectID format'
raise MongoMapper::DocumentNotFound
else
raise exception
end
end
end
And the code in mongodb-mongo-0.10.1/lib/mongo/types/objectid.rb:
# Given a string representation of an ObjectID, return a new
ObjectID
# with that value.
def self.from_string(str)
raise "illegal ObjectID format" unless legal?(str)
data = []
BYTE_ORDER.each_with_index { |string_position, data_index|
data[data_index] = str[string_position * 2, 2].to_i(16)
}
self.new(data)
end