From db1f3821509d3fb5535444cd01747aaac4972bfd Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Wed, 11 Nov 2009 10:47:42 -0500 Subject: [PATCH] Added #hash method for ObjectID. --- README.rdoc | 3 +++ lib/mongo/types/objectid.rb | 6 ++++++ test/test_objectid.rb | 13 ++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 852c6c5..e7f2a70 100644 --- a/README.rdoc +++ b/README.rdoc @@ -356,6 +356,9 @@ Les Hill, leshill on github Sean Cribbs, seancribbs on github * Modify standard_benchmark to allow profiling +Sunny Hirai +* Suggested hashcode fix for Mongo::ObjectID + = License Copyright 2008-2009 10gen Inc. diff --git a/lib/mongo/types/objectid.rb b/lib/mongo/types/objectid.rb index 8bc2b0d..9d41018 100644 --- a/lib/mongo/types/objectid.rb +++ b/lib/mongo/types/objectid.rb @@ -57,6 +57,12 @@ module Mongo end alias_method :==, :eql? + # Returns a unique hashcode for the object. + # This is required since we've defined an #eql? method. + def hash + @data.hash + end + def to_a @data.dup end diff --git a/test/test_objectid.rb b/test/test_objectid.rb index f5a2336..bc0d405 100644 --- a/test/test_objectid.rb +++ b/test/test_objectid.rb @@ -7,7 +7,18 @@ class ObjectIDTest < Test::Unit::TestCase include Mongo def setup - @o = ObjectID.new() + @o = ObjectID.new + end + + def test_hashcode + assert_equal @o.instance_variable_get(:@data).hash, @o.hash + end + + def test_array_uniq_for_equilavent_ids + a = ObjectID.new('123') + b = ObjectID.new('123') + assert_equal a, b + assert_equal 1, [a, b].uniq.size end def test_create_pk_method