adapter-couch 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/examples/couch.rb +1 -1
- data/lib/adapter/couch.rb +15 -2
- data/lib/adapter/couch/version.rb +1 -1
- data/spec/couch_spec.rb +13 -3
- data/spec/helper.rb +2 -2
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/examples/couch.rb
CHANGED
data/lib/adapter/couch.rb
CHANGED
@@ -8,6 +8,7 @@ module Adapter
|
|
8
8
|
def read(key)
|
9
9
|
begin
|
10
10
|
if doc = client.get(key_for(key))
|
11
|
+
rev_map[key_for(key)] = doc.delete("_rev")
|
11
12
|
decode(doc)
|
12
13
|
end
|
13
14
|
rescue RestClient::ResourceNotFound => e
|
@@ -16,8 +17,11 @@ module Adapter
|
|
16
17
|
end
|
17
18
|
|
18
19
|
def write(key, value)
|
19
|
-
doc =
|
20
|
-
doc
|
20
|
+
doc = {"_id" => key_for(key)}.merge(encode(value))
|
21
|
+
doc["_rev"] = rev_map[key_for(key)] if rev_map[key_for(key)]
|
22
|
+
status = client.save_doc(doc)
|
23
|
+
rev_map[key_for(key)] = status["rev"]
|
24
|
+
value
|
21
25
|
end
|
22
26
|
|
23
27
|
def delete(key)
|
@@ -42,6 +46,15 @@ module Adapter
|
|
42
46
|
return if value.nil?
|
43
47
|
value.key?(NonHashValueKeyName) ? value[NonHashValueKeyName] : value
|
44
48
|
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# we want to hide couchdb's concept of revs behind the scenes.
|
53
|
+
# This is probably not the best way of doing it and we should clear it out after each request like
|
54
|
+
# ToyStore's identity map
|
55
|
+
def rev_map
|
56
|
+
Thread.current[:couch_adapter_rev_map] ||= {}
|
57
|
+
end
|
45
58
|
|
46
59
|
end
|
47
60
|
end
|
data/spec/couch_spec.rb
CHANGED
@@ -14,8 +14,18 @@ describe "Couch adapter" do
|
|
14
14
|
it_should_behave_like 'a couch adapter'
|
15
15
|
|
16
16
|
it "stores hashes right in document" do
|
17
|
-
|
18
|
-
client.get('foo').should == {'_id' => 'foo', '_rev' =>
|
17
|
+
adapter.write('foo', 'steak' => 'bacon')
|
18
|
+
client.get('foo').should == {'_id' => 'foo', '_rev' => adapter.send(:rev_map)['foo'], 'steak' => 'bacon'}
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should save with rev" do
|
22
|
+
adapter.write('foo', 'steak' => 'bacon')
|
23
|
+
lambda { adapter.write('foo', 'steak' => 'ham')}.should_not raise_error(RestClient::Conflict)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should load existing and save" do
|
27
|
+
client.save_doc("_id"=>"foo", "steak" => "bacon")
|
28
|
+
doc = adapter.get('foo')
|
29
|
+
lambda { adapter.write('foo', 'steak' => 'ham')}.should_not raise_error(RestClient::Conflict)
|
19
30
|
end
|
20
|
-
|
21
31
|
end
|
data/spec/helper.rb
CHANGED
@@ -23,9 +23,9 @@ shared_examples_for "a couch adapter" do
|
|
23
23
|
|
24
24
|
Adapter::Spec::Types.each do |type, (key, key2)|
|
25
25
|
it "writes Object values to keys that are #{type}s like a Hash" do
|
26
|
-
|
26
|
+
adapter.write(key, {:foo => :bar})
|
27
27
|
# couch knows hashes
|
28
|
-
adapter[key].should == {'_id' => 'key', 'foo' => 'bar'
|
28
|
+
adapter[key].should == {'_id' => 'key', 'foo' => 'bar'}
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: adapter-couch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Pelle Braendgaard
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-22 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|