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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- adapter-couch (0.1.1)
4
+ adapter-couch (0.1.2)
5
5
  adapter (~> 0.5.1)
6
6
  couchrest (~> 1.1.0.pre2)
7
7
 
@@ -7,7 +7,7 @@ $:.unshift(lib_path)
7
7
 
8
8
  require 'adapter/couch'
9
9
 
10
- client = Couch::Connection.new.db('adapter')['testing']
10
+ client = CouchRest.database!("http://127.0.0.1:5984/adapter-couch-testing")
11
11
  adapter = Adapter[:couch].new(client)
12
12
  adapter.clear
13
13
 
@@ -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 = client.save_doc({"_id" => key_for(key)}.merge(encode(value)))
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
@@ -1,5 +1,5 @@
1
1
  module Adapter
2
2
  module Couch
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
@@ -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
- doc = adapter.write('foo', 'steak' => 'bacon')
18
- client.get('foo').should == {'_id' => 'foo', '_rev' => doc['rev'], 'steak' => 'bacon'}
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
@@ -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
- v = adapter.write(key, {:foo => :bar})
26
+ adapter.write(key, {:foo => :bar})
27
27
  # couch knows hashes
28
- adapter[key].should == {'_id' => 'key', 'foo' => 'bar', '_rev' => v['rev']}
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.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-21 00:00:00 -04:00
13
+ date: 2011-05-22 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency