gom-couchdb-adapter 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,19 +24,22 @@ class GOM::Storage::CouchDB::Adapter < GOM::Storage::Adapter
24
24
  def store(draft)
25
25
  saver = GOM::Storage::CouchDB::Saver.new @database, draft, revisions, configuration.name
26
26
  saver.perform
27
- saver.id
27
+ saver.object_id
28
28
  end
29
29
 
30
30
  def remove(id)
31
- remover = GOM::Storage::CouchDB::Remover.new @database, id, revisions
32
- remover.perform
31
+ GOM::Storage::CouchDB::Remover.new(@database, id, revisions).perform
32
+ end
33
+
34
+ def count
35
+ GOM::Storage::CouchDB::Counter.new(@database).perform
33
36
  end
34
37
 
35
38
  def collection(name, options = { })
36
39
  view = @design.views[name.to_s]
37
40
  raise ViewNotFoundError, "there are no view with the name #{name}" unless view
38
41
  fetcher = GOM::Storage::CouchDB::Collection::Fetcher.new view, options
39
- GOM::Object::Collection.new fetcher
42
+ GOM::Object::Collection.new fetcher, configuration.name
40
43
  end
41
44
 
42
45
  def revisions
@@ -0,0 +1,27 @@
1
+
2
+ # Fetches the databases document count.
3
+ class GOM::Storage::CouchDB::Counter
4
+
5
+ attr_accessor :database
6
+
7
+ def initialize(database)
8
+ @database = database
9
+ end
10
+
11
+ def perform
12
+ fetch_information
13
+ fetch_count
14
+ @count
15
+ end
16
+
17
+ private
18
+
19
+ def fetch_information
20
+ @information = @database.information
21
+ end
22
+
23
+ def fetch_count
24
+ @count = @information["doc_count"]
25
+ end
26
+
27
+ end
@@ -8,7 +8,7 @@ class GOM::Storage::CouchDB::Draft::Builder
8
8
 
9
9
  def draft
10
10
  initialize_draft
11
- set_id
11
+ set_object_id
12
12
  set_class
13
13
  set_properties_and_relations
14
14
  @draft
@@ -20,8 +20,8 @@ class GOM::Storage::CouchDB::Draft::Builder
20
20
  @draft = GOM::Object::Draft.new
21
21
  end
22
22
 
23
- def set_id
24
- @draft.id = @document.id
23
+ def set_object_id
24
+ @draft.object_id = @document.id
25
25
  end
26
26
 
27
27
  def set_class
@@ -50,7 +50,7 @@ class GOM::Storage::CouchDB::Draft::Builder
50
50
  end
51
51
 
52
52
  def relation_key?(key)
53
- key =~ /.+_id$/
53
+ !!(key =~ /.+_id$/)
54
54
  end
55
55
 
56
56
  end
@@ -2,10 +2,10 @@
2
2
  # Saves the given draft to a CouchDB document.
3
3
  class GOM::Storage::CouchDB::Saver
4
4
 
5
- attr_reader :id
5
+ attr_reader :object_id
6
6
 
7
- def initialize(database, draft, revisions, relation_storage_name)
8
- @database, @draft, @revisions, @relation_storage_name = database, draft, revisions, relation_storage_name
7
+ def initialize(database, draft, revisions, storage_name)
8
+ @database, @draft, @revisions, @storage_name = database, draft, revisions, storage_name
9
9
  end
10
10
 
11
11
  def perform
@@ -20,8 +20,11 @@ class GOM::Storage::CouchDB::Saver
20
20
 
21
21
  def initialize_document
22
22
  @document = ::CouchDB::Document.new @database
23
- draft_id = @draft.id
24
- @document.id = draft_id if draft_id
23
+ object_id = @draft.object_id
24
+ if object_id
25
+ @document.id = object_id
26
+ @document.rev = @revisions[object_id]
27
+ end
25
28
  @document["model_class"] = @draft.class_name
26
29
  end
27
30
 
@@ -37,7 +40,7 @@ class GOM::Storage::CouchDB::Saver
37
40
  @document["#{key}_id"] = if id
38
41
  id.to_s
39
42
  else
40
- GOM::Storage.store object, @relation_storage_name
43
+ GOM::Storage.store object, @storage_name
41
44
  GOM::Object.id object
42
45
  end
43
46
  end
@@ -45,7 +48,7 @@ class GOM::Storage::CouchDB::Saver
45
48
 
46
49
  def save_document
47
50
  @document.save
48
- @id = @document.id
51
+ @object_id = @document.id
49
52
  end
50
53
 
51
54
  def store_revision
@@ -3,6 +3,7 @@ module GOM::Storage::CouchDB
3
3
 
4
4
  autoload :Adapter, File.join(File.dirname(__FILE__), "couchdb", "adapter")
5
5
  autoload :Collection, File.join(File.dirname(__FILE__), "couchdb", "collection")
6
+ autoload :Counter, File.join(File.dirname(__FILE__), "couchdb", "counter")
6
7
  autoload :Fetcher, File.join(File.dirname(__FILE__), "couchdb", "fetcher")
7
8
  autoload :Draft, File.join(File.dirname(__FILE__), "couchdb", "draft")
8
9
  autoload :Saver, File.join(File.dirname(__FILE__), "couchdb", "saver")
@@ -114,9 +114,9 @@ describe GOM::Storage::CouchDB::Adapter do
114
114
 
115
115
  @draft = mock GOM::Object::Draft
116
116
  @revisions = @adapter.send :revisions
117
- @id = "test_object_1"
117
+ @object_id = "object_1"
118
118
 
119
- @saver = mock GOM::Storage::CouchDB::Saver, :perform => nil, :id => @id
119
+ @saver = mock GOM::Storage::CouchDB::Saver, :perform => nil, :object_id => @object_id
120
120
  GOM::Storage::CouchDB::Saver.stub(:new).and_return(@saver)
121
121
  end
122
122
 
@@ -131,7 +131,7 @@ describe GOM::Storage::CouchDB::Adapter do
131
131
  end
132
132
 
133
133
  it "should return the draft" do
134
- @adapter.store(@draft).should == @id
134
+ @adapter.store(@draft).should == @object_id
135
135
  end
136
136
 
137
137
  end
@@ -160,6 +160,32 @@ describe GOM::Storage::CouchDB::Adapter do
160
160
 
161
161
  end
162
162
 
163
+ describe "count" do
164
+
165
+ before :each do
166
+ @adapter.setup
167
+
168
+ @counter = mock GOM::Storage::CouchDB::Counter, :perform => 1
169
+ GOM::Storage::CouchDB::Counter.stub(:new).and_return(@counter)
170
+ end
171
+
172
+ it "should initialize the counter" do
173
+ GOM::Storage::CouchDB::Counter.should_receive(:new).with(@database).and_return(@counter)
174
+ @adapter.count
175
+ end
176
+
177
+ it "should perform a count" do
178
+ @counter.should_receive(:perform)
179
+ @adapter.count
180
+ end
181
+
182
+ it "should return the count" do
183
+ count = @adapter.count
184
+ count.should == 1
185
+ end
186
+
187
+ end
188
+
163
189
  describe "collection" do
164
190
 
165
191
  before :each do
@@ -196,7 +222,7 @@ describe GOM::Storage::CouchDB::Adapter do
196
222
  end
197
223
 
198
224
  it "should initialize a collection with the fetcher" do
199
- GOM::Object::Collection.should_receive(:new).with(@fetcher).and_return(@collection)
225
+ GOM::Object::Collection.should_receive(:new).with(@fetcher, "test_storage").and_return(@collection)
200
226
  @adapter.collection :test_view, @options
201
227
  end
202
228
 
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe GOM::Storage::CouchDB::Counter do
4
+
5
+ before :each do
6
+ @database = mock CouchDB::Database, :information => { "doc_count" => 1 }
7
+
8
+ @counter = described_class.new @database
9
+ end
10
+
11
+ describe "perform" do
12
+
13
+ it "should fetch database information" do
14
+ @counter.perform
15
+ end
16
+
17
+ it "should return the document count" do
18
+ count = @counter.perform
19
+ count.should == 1
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -10,9 +10,9 @@ describe GOM::Storage::CouchDB::Draft::Builder do
10
10
 
11
11
  describe "draft" do
12
12
 
13
- it "should set the id" do
13
+ it "should set the object id" do
14
14
  draft = @builder.draft
15
- draft.id.should == "test_document_1"
15
+ draft.object_id.should == "test_document_1"
16
16
  end
17
17
 
18
18
  it "should set the class" do
@@ -4,17 +4,17 @@ describe GOM::Storage::CouchDB::Saver do
4
4
 
5
5
  before :each do
6
6
  @database = mock CouchDB::Database
7
- @draft = GOM::Object::Draft.new "test_object_1"
8
- @revisions = mock Hash, :[]= => nil
9
- @relation_storage_name = "test_storage"
7
+ @draft = GOM::Object::Draft.new "object_1"
8
+ @revisions = { "object_1" => 1 }
9
+ @storage_name = "test_storage"
10
10
 
11
- @saver = described_class.new @database, @draft, @revisions, @relation_storage_name
11
+ @saver = described_class.new @database, @draft, @revisions, @storage_name
12
12
  end
13
13
 
14
14
  describe "perform" do
15
15
 
16
16
  before :each do
17
- @document = mock CouchDB::Document, :[]= => nil, :id= => nil, :save => true, :id => "test_object_1", :rev => 1
17
+ @document = mock CouchDB::Document, :[]= => nil, :id= => nil, :save => true, :id => "object_1", :rev => 2, :rev= => nil
18
18
  CouchDB::Document.stub(:new).and_return(@document)
19
19
  end
20
20
 
@@ -23,6 +23,38 @@ describe GOM::Storage::CouchDB::Saver do
23
23
  @saver.perform
24
24
  end
25
25
 
26
+ it "should set the id" do
27
+ @document.should_receive(:id=).with("object_1")
28
+ @saver.perform
29
+ end
30
+
31
+ it "should set the revision" do
32
+ @document.should_receive(:rev=).with(1)
33
+ @saver.perform
34
+ end
35
+
36
+ it "should not set the id or revision if not included in the draft" do
37
+ @document.should_not_receive(:id=)
38
+ @document.should_not_receive(:rev=)
39
+ @draft.object_id = nil
40
+ @saver.perform
41
+ end
42
+
43
+ it "should save the document" do
44
+ @document.should_receive(:save).and_return(true)
45
+ @saver.perform
46
+ end
47
+
48
+ it "should store the revision" do
49
+ @saver.perform
50
+ @revisions.should == { "object_1" => 2 }
51
+ end
52
+
53
+ it "should return the (new) object id" do
54
+ @saver.perform
55
+ @saver.object_id.should == "object_1"
56
+ end
57
+
26
58
  context "draft with properties" do
27
59
 
28
60
  before :each do
@@ -40,7 +72,7 @@ describe GOM::Storage::CouchDB::Saver do
40
72
 
41
73
  before :each do
42
74
  @related_object = Object.new
43
- @related_object_id = mock GOM::Object::Id, :to_s => "test_storage:test_object_2"
75
+ @related_object_id = mock GOM::Object::Id, :to_s => "test_storage:object_2"
44
76
  @related_object_proxy = mock GOM::Object::Proxy, :id => @related_object_id, :object => @related_object
45
77
 
46
78
  GOM::Storage.stub(:store)
@@ -50,7 +82,7 @@ describe GOM::Storage::CouchDB::Saver do
50
82
  end
51
83
 
52
84
  it "should set the relations" do
53
- @document.should_receive(:[]=).with("related_object_id", "test_storage:test_object_2")
85
+ @document.should_receive(:[]=).with("related_object_id", "test_storage:object_2")
54
86
  @saver.perform
55
87
  end
56
88
 
@@ -66,32 +98,6 @@ describe GOM::Storage::CouchDB::Saver do
66
98
 
67
99
  end
68
100
 
69
- it "should set the id" do
70
- @document.should_receive(:id=).with("test_object_1")
71
- @saver.perform
72
- end
73
-
74
- it "should not set the id if not included in the draft" do
75
- @document.should_not_receive(:id=)
76
- @draft.id = nil
77
- @saver.perform
78
- end
79
-
80
- it "should save the document" do
81
- @document.should_receive(:save).and_return(true)
82
- @saver.perform
83
- end
84
-
85
- it "should store the revision" do
86
- @revisions.should_receive(:[]=).with("test_object_1", 1)
87
- @saver.perform
88
- end
89
-
90
- it "should return the (new) object id" do
91
- @saver.perform
92
- @saver.id.should == "test_object_1"
93
- end
94
-
95
101
  end
96
102
 
97
103
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Philipp Br\xC3\xBCll"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-17 00:00:00 +01:00
17
+ date: 2011-03-07 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -28,8 +28,8 @@ dependencies:
28
28
  segments:
29
29
  - 0
30
30
  - 3
31
- - 0
32
- version: 0.3.0
31
+ - 1
32
+ version: 0.3.1
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
@@ -86,32 +86,34 @@ files:
86
86
  - README.rdoc
87
87
  - LICENSE
88
88
  - Rakefile
89
- - lib/gom/couchdb-adapter.rb
90
- - lib/gom/storage.rb
91
- - lib/gom/storage/couchdb.rb
92
- - lib/gom/storage/couchdb/fetcher.rb
93
- - lib/gom/storage/couchdb/draft/builder.rb
89
+ - lib/gom/storage/couchdb/adapter.rb
90
+ - lib/gom/storage/couchdb/view/builder.rb
91
+ - lib/gom/storage/couchdb/view/pusher.rb
92
+ - lib/gom/storage/couchdb/counter.rb
94
93
  - lib/gom/storage/couchdb/view.rb
95
94
  - lib/gom/storage/couchdb/saver.rb
95
+ - lib/gom/storage/couchdb/fetcher.rb
96
+ - lib/gom/storage/couchdb/draft/builder.rb
97
+ - lib/gom/storage/couchdb/draft.rb
96
98
  - lib/gom/storage/couchdb/remover.rb
97
99
  - lib/gom/storage/couchdb/collection/fetcher.rb
98
100
  - lib/gom/storage/couchdb/collection.rb
99
- - lib/gom/storage/couchdb/view/pusher.rb
100
- - lib/gom/storage/couchdb/view/builder.rb
101
- - lib/gom/storage/couchdb/draft.rb
102
- - lib/gom/storage/couchdb/adapter.rb
103
- - spec/storage.configuration
101
+ - lib/gom/storage/couchdb.rb
102
+ - lib/gom/couchdb-adapter.rb
103
+ - lib/gom/storage.rb
104
+ - spec/lib/gom/storage/couchdb/fetcher_spec.rb
105
+ - spec/lib/gom/storage/couchdb/view/pusher_spec.rb
106
+ - spec/lib/gom/storage/couchdb/view/builder_spec.rb
104
107
  - spec/lib/gom/storage/couchdb/draft/builder_spec.rb
108
+ - spec/lib/gom/storage/couchdb/counter_spec.rb
105
109
  - spec/lib/gom/storage/couchdb/collection/fetcher_spec.rb
106
- - spec/lib/gom/storage/couchdb/adapter_spec.rb
107
- - spec/lib/gom/storage/couchdb/view/builder_spec.rb
108
- - spec/lib/gom/storage/couchdb/view/pusher_spec.rb
109
- - spec/lib/gom/storage/couchdb/fetcher_spec.rb
110
- - spec/lib/gom/storage/couchdb/saver_spec.rb
111
110
  - spec/lib/gom/storage/couchdb/remover_spec.rb
112
- - spec/acceptance/adapter_spec.rb
113
- - spec/acceptance/map_reduce_spec.rb
111
+ - spec/lib/gom/storage/couchdb/saver_spec.rb
112
+ - spec/lib/gom/storage/couchdb/adapter_spec.rb
113
+ - spec/storage.configuration
114
114
  - spec/spec_helper.rb
115
+ - spec/acceptance/map_reduce_spec.rb
116
+ - spec/acceptance/adapter_spec.rb
115
117
  has_rdoc: true
116
118
  homepage: http://github.com/phifty/gom-couchdb-adapter
117
119
  licenses: []
@@ -145,13 +147,14 @@ signing_key:
145
147
  specification_version: 3
146
148
  summary: CouchDB storage adapter for the General Object Mapper.
147
149
  test_files:
150
+ - spec/lib/gom/storage/couchdb/fetcher_spec.rb
151
+ - spec/lib/gom/storage/couchdb/view/pusher_spec.rb
152
+ - spec/lib/gom/storage/couchdb/view/builder_spec.rb
148
153
  - spec/lib/gom/storage/couchdb/draft/builder_spec.rb
154
+ - spec/lib/gom/storage/couchdb/counter_spec.rb
149
155
  - spec/lib/gom/storage/couchdb/collection/fetcher_spec.rb
150
- - spec/lib/gom/storage/couchdb/adapter_spec.rb
151
- - spec/lib/gom/storage/couchdb/view/builder_spec.rb
152
- - spec/lib/gom/storage/couchdb/view/pusher_spec.rb
153
- - spec/lib/gom/storage/couchdb/fetcher_spec.rb
154
- - spec/lib/gom/storage/couchdb/saver_spec.rb
155
156
  - spec/lib/gom/storage/couchdb/remover_spec.rb
156
- - spec/acceptance/adapter_spec.rb
157
+ - spec/lib/gom/storage/couchdb/saver_spec.rb
158
+ - spec/lib/gom/storage/couchdb/adapter_spec.rb
157
159
  - spec/acceptance/map_reduce_spec.rb
160
+ - spec/acceptance/adapter_spec.rb