Skip to content

Commit

Permalink
Updated for latest adapter.
Browse files Browse the repository at this point in the history
No more support for storing arbitrary values. Now only an id and a Hash of attributes will work.
  • Loading branch information
jnunemaker committed Nov 7, 2012
1 parent af0d96d commit 58f3674
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 34 deletions.
2 changes: 1 addition & 1 deletion adapter-mongo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency 'adapter', '~> 0.5'
s.add_dependency 'adapter', '~> 0.6.1'
s.add_dependency 'mongo', '~> 1.5'
end
19 changes: 2 additions & 17 deletions lib/adapter/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

module Adapter
module Mongo
NonHashValueKeyName = '_value'

def read(key)
if doc = client.find_one('_id' => key_for(key))
decode(doc)
Expand All @@ -23,22 +21,9 @@ def clear
client.remove
end

def key_for(key)
case key
when BSON::ObjectId, Hash
key
else
super
end
end

def encode(value)
value.is_a?(Hash) ? value : {NonHashValueKeyName => value}
end

def decode(value)
return if value.nil?
value.key?(NonHashValueKeyName) ? value[NonHashValueKeyName] : value
value.delete('_id')
value
end
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Bundler.require(:default, :test)

require 'adapter/spec/an_adapter'
require 'adapter/spec/types'
require 'adapter-mongo'

require 'support/shared_mongo_adapter'
Expand Down
1 change: 0 additions & 1 deletion spec/mongo_atomic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
adapter.write(oid, {'a' => 'c', 'b' => 'd'})
adapter.write(oid, {'a' => 'z'})
adapter.read(oid).should eq({
'_id' => oid,
'a' => 'z',
'b' => 'd',
})
Expand Down
23 changes: 9 additions & 14 deletions spec/support/shared_mongo_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
shared_examples_for "a mongo adapter" do
it_should_behave_like 'an adapter'

Adapter::Spec::Types.each do |type, (key, key2)|
it "writes Object values to keys that are #{type}s like a Hash" do
adapter[key] = {:foo => :bar}
# mongo knows hashes and can serialize symbol values
adapter[key].should == {'_id' => 'key', 'foo' => :bar}
end
end

it "allows using object id's as keys in correct type" do
id = BSON::ObjectId.new
adapter.write(id, 'ham')
attributes = {'one' => 'two'}
adapter.write(id, attributes)
client.find_one('_id' => id).should_not be_nil
adapter.read(id).should == 'ham'
adapter.read(id).should == attributes
end

it "allows using ordered hashes as keys" do
key = BSON::OrderedHash['d', 1, 'n', 1]
adapter.write(key, 'ham')
attributes = {'one' => 'two'}
adapter.write(key, attributes)
client.find_one('_id' => key).should_not be_nil
adapter.read(key).should == 'ham'
adapter.read(key).should == attributes
end

it "allows using hashes as keys" do
key = {:d => 1}
adapter.write(key, 'ham')
attributes = {'one' => 'two'}
adapter.write(key, attributes)
client.find_one('_id' => key).should_not be_nil
adapter.read(key).should == 'ham'
adapter.read(key).should == attributes
end

it "stores hashes right in document" do
Expand Down

0 comments on commit 58f3674

Please sign in to comment.