Permalink
Browse files

Allow using hashes as keys.

Fixes #1.
  • Loading branch information...
1 parent 1f6ac4e commit 1c5b153fdcef66656f0283a67c0e54e2940ba82d @jnunemaker committed Jun 25, 2012
Showing with 21 additions and 2 deletions.
  1. +7 −2 lib/adapter/mongo.rb
  2. +14 −0 spec/support/shared_mongo_adapter.rb
View
@@ -24,7 +24,12 @@ def clear
end
def key_for(key)
- key.is_a?(BSON::ObjectId) ? key : super
+ case key
+ when BSON::ObjectId, Hash
+ key
+ else
+ super
+ end
end
def encode(value)
@@ -38,4 +43,4 @@ def decode(value)
end
end
-Adapter.define(:mongo, Adapter::Mongo)
+Adapter.define(:mongo, Adapter::Mongo)
@@ -16,6 +16,20 @@
adapter.read(id).should == 'ham'
end
+ it "allows using ordered hashes as keys" do
+ key = BSON::OrderedHash['d', 1, 'n', 1]
+ adapter.write(key, 'ham')
+ client.find_one('_id' => key).should_not be_nil
+ adapter.read(key).should == 'ham'
+ end
+
+ it "allows using hashes as keys" do
+ key = {:d => 1}
+ adapter.write(key, 'ham')
+ client.find_one('_id' => key).should_not be_nil
+ adapter.read(key).should == 'ham'
+ end
+
it "stores hashes right in document" do
adapter.write('foo', 'steak' => 'bacon')
client.find_one('_id' => 'foo').should == {'_id' => 'foo', 'steak' => 'bacon'}

0 comments on commit 1c5b153

Please sign in to comment.