relaxdb 0.3.5 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/README.textile +21 -23
  2. data/Rakefile +2 -7
  3. data/docs/spec_results.html +5 -5
  4. data/lib/more/grapher.rb +1 -1
  5. data/lib/relaxdb.rb +3 -5
  6. data/lib/relaxdb/all_delegator.rb +19 -13
  7. data/lib/relaxdb/document.rb +150 -218
  8. data/lib/relaxdb/extlib.rb +7 -1
  9. data/lib/relaxdb/migration.rb +11 -8
  10. data/lib/relaxdb/net_http_server.rb +19 -1
  11. data/lib/relaxdb/paginator.rb +30 -11
  12. data/lib/relaxdb/query.rb +1 -1
  13. data/lib/relaxdb/{belongs_to_proxy.rb → references_proxy.rb} +3 -3
  14. data/lib/relaxdb/relaxdb.rb +87 -7
  15. data/lib/relaxdb/server.rb +8 -2
  16. data/lib/relaxdb/taf2_curb_server.rb +2 -1
  17. data/lib/relaxdb/uuid_generator.rb +38 -2
  18. data/lib/relaxdb/view_by_delegator.rb +34 -0
  19. data/lib/relaxdb/view_object.rb +1 -1
  20. data/lib/relaxdb/view_uploader.rb +16 -2
  21. data/lib/relaxdb/views.rb +23 -55
  22. data/readme.rb +3 -3
  23. data/spec/all_delegator_spec.rb +52 -0
  24. data/spec/callbacks_spec.rb +4 -4
  25. data/spec/derived_properties_spec.rb +4 -4
  26. data/spec/design_doc_spec.rb +2 -2
  27. data/spec/doc_inheritable_spec.rb +2 -2
  28. data/spec/document_spec.rb +47 -25
  29. data/spec/migration_spec.rb +12 -10
  30. data/spec/qpaginate_spec.rb +88 -0
  31. data/spec/query_spec.rb +2 -2
  32. data/spec/references_proxy_spec.rb +94 -0
  33. data/spec/relaxdb_spec.rb +29 -21
  34. data/spec/server_spec.rb +4 -3
  35. data/spec/spec_helper.rb +1 -0
  36. data/spec/spec_models.rb +48 -57
  37. data/spec/uuid_generator_spec.rb +34 -0
  38. data/spec/view_by_spec.rb +62 -54
  39. data/spec/view_docs_by_spec.rb +85 -0
  40. metadata +38 -27
  41. data/lib/more/atomic_bulk_save_support.rb +0 -18
  42. data/lib/relaxdb/has_many_proxy.rb +0 -101
  43. data/lib/relaxdb/has_one_proxy.rb +0 -42
  44. data/lib/relaxdb/references_many_proxy.rb +0 -97
  45. data/spec/belongs_to_spec.rb +0 -124
  46. data/spec/has_many_spec.rb +0 -202
  47. data/spec/has_one_spec.rb +0 -123
  48. data/spec/references_many_spec.rb +0 -173
  49. data/spec/view_spec.rb +0 -23
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require File.dirname(__FILE__) + '/spec_models.rb'
3
+
4
+ describe RelaxDB::UuidGenerator do
5
+
6
+ before(:all) do
7
+ setup_test_db
8
+ @ug = RelaxDB::UuidGenerator
9
+ end
10
+
11
+ before(:each) do
12
+ @ug.reset
13
+ end
14
+
15
+ it "should retrieve UUIDs from CouchDB" do
16
+ RelaxDB.db.reset_req_count
17
+ @ug.uuid
18
+ RelaxDB.db.get_count.should == 1
19
+ end
20
+
21
+ it "should retrieve count number of UUIDs" do
22
+ RelaxDB.db.reset_req_count
23
+ @ug.count = 1
24
+ 3.times { @ug.uuid }
25
+ RelaxDB.db.get_count.should == 3
26
+ end
27
+
28
+ it "should retrieve n UUIDs from CouchDB in a single request" do
29
+ RelaxDB.db.reset_req_count
30
+ 100.times { @ug.uuid }
31
+ RelaxDB.db.get_count.should == 1
32
+ end
33
+
34
+ end
data/spec/view_by_spec.rb CHANGED
@@ -3,74 +3,82 @@ require File.dirname(__FILE__) + '/spec_models.rb'
3
3
 
4
4
  describe "view_by" do
5
5
 
6
- before(:all) do
7
- RelaxDB.configure :host => "localhost", :port => 5984, :design_doc => "spec_doc"
6
+ before(:each) do
7
+ setup_test_db
8
+
9
+ docs = (1..10).map { |i| Primitives.new :_id => "id#{i}", :str => i.to_s }
10
+ RelaxDB.bulk_save! *docs
8
11
  end
9
12
 
10
- describe "view_by" do
13
+ it "should return an array of doc ids for a key" do
14
+ docs = Primitives.by_str :key => "5"
15
+ docs.first.should == "id5"
16
+ end
17
+
18
+ it "should obey startkey and endkey directives" do
19
+ docs = Primitives.by_str :startkey => "3", :endkey => "6"
20
+ docs.should == %w(id3 id4 id5 id6)
21
+ end
22
+
23
+ it "should return all when none specified" do
24
+ docs = Primitives.by_str
25
+ docs.size.should == 10
26
+ end
27
+
28
+ it "should return arrays that behave normally" do
29
+ p1 = Primitives.by_str :key => "1"
30
+ p2 = Primitives.by_str :key => "2"
31
+ RelaxDB.load!(p1 + p2).map { |p| p.str }.join.should == "12"
32
+ end
33
+
34
+ describe "delegator" do
11
35
 
12
- before(:each) do
13
- RelaxDB.delete_db "relaxdb_spec" rescue "ok"
14
- RelaxDB.use_db "relaxdb_spec"
15
- RelaxDB.enable_view_creation
16
-
17
- class ViewByFoo < RelaxDB::Document
18
- property :foo
19
- view_by :foo, :descending => true
20
- end
21
-
36
+ it "should load the returned doc ids" do
37
+ docs = Primitives.by_str :key => "5"
38
+ docs.load!.first.str.should == "5"
22
39
  end
23
40
 
24
- it "should create corresponding views" do
25
- dd = RelaxDB::DesignDocument.get "spec_doc"
26
- dd.data["views"]["ViewByFoo_by_foo"].should be
41
+ it "should load the doc for a single param" do
42
+ res = Primitives.by_str "8"
43
+ res.str.should == "8"
27
44
  end
28
-
29
- it "should create a by_ att list method" do
30
- ViewByFoo.new(:foo => :bar).save!
31
- res = ViewByFoo.by_foo
32
- res.first.foo.should == "bar"
33
- end
34
-
35
- it "should create a paginate_by_ att list method" do
36
- ViewByFoo.new(:foo => :bar).save!
37
- res = ViewByFoo.paginate_by_foo :page_params => {}, :startkey => {}, :endkey => nil
38
- res.first.foo.should == "bar"
45
+
46
+ it "should return the delegator when no params given" do
47
+ docs = Primitives.by_str.load!
48
+ docs.map { |d| d.str }.join.length.should == 11
39
49
  end
40
50
 
41
- it "should apply query defaults to by_" do
42
- ViewByFoo.new(:foo => "a").save!
43
- ViewByFoo.new(:foo => "b").save!
44
-
45
- ViewByFoo.by_foo.map{ |o| o.foo }.should == ["b", "a"]
46
- end
51
+ end
52
+
53
+ describe "request count" do
47
54
 
48
- it "should allow a single arg to be passed to by_" do
49
- vbf = ViewByFoo.new(:foo => "a").save!
50
- ViewByFoo.by_foo("a").should == vbf
55
+ before(:each) do
56
+ RelaxDB.db.reset_req_count
51
57
  end
52
58
 
53
- it "should apply query defaults to paginate_by_" do
54
- ViewByFoo.new(:foo => "a").save!
55
- ViewByFoo.new(:foo => "b").save!
56
-
57
- res = ViewByFoo.paginate_by_foo :page_params => {}, :startkey => {}, :endkey => nil
58
- res.map{ |o| o.foo }.should == ["b", "a"]
59
+ it "should not issue any requests before accessingn the delegator" do
60
+ doc_ids = Primitives.by_str
61
+ RelaxDB.db.req_count.should == 0
59
62
  end
60
-
61
- it "should allow query defaults to be overridden for paginate_by_" do
62
- ViewByFoo.new(:foo => :bar).save!
63
- res = ViewByFoo.paginate_by_foo :page_params => {}, :startkey => nil, :endkey => {}, :descending => false
64
- res.first.foo.should == "bar"
63
+
64
+ it "should issue a single request when retrieving ids only" do
65
+ doc_ids = Primitives.by_str
66
+ doc_ids[0]
67
+ RelaxDB.db.req_count.should == 1
65
68
  end
66
69
 
67
- it "should allow query defaults to be overridden for by_" do
68
- ViewByFoo.new(:foo => "a").save!
69
- ViewByFoo.new(:foo => "b").save!
70
-
71
- ViewByFoo.by_foo(:descending => false).map{ |o| o.foo }.should == ["a", "b"]
70
+ it "should make two requests when loading docs after touching ids" do
71
+ doc_ids = Primitives.by_str
72
+ doc_ids[0].should == "id1"
73
+ doc_ids.load!
74
+ RelaxDB.db.req_count.should == 2
72
75
  end
73
-
76
+
77
+ it "should issue a single request when retrieving docs directly" do
78
+ Primitives.by_str.load!
79
+ RelaxDB.db.req_count.should == 1
80
+ end
81
+
74
82
  end
75
83
 
76
- end
84
+ end
@@ -0,0 +1,85 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require File.dirname(__FILE__) + '/spec_models.rb'
3
+
4
+ describe "view_docs_by" do
5
+
6
+ before(:all) do
7
+ RelaxDB.configure :host => "localhost", :port => 5984, :design_doc => "spec_doc"
8
+ end
9
+
10
+ describe "view_docs_by" do
11
+
12
+ before(:each) do
13
+ RelaxDB.delete_db "relaxdb_spec" rescue "ok"
14
+ RelaxDB.use_db "relaxdb_spec"
15
+ RelaxDB.enable_view_creation
16
+
17
+ class ::ViewByFoo < RelaxDB::Document
18
+ property :foo
19
+ view_docs_by :foo, :descending => true
20
+ end
21
+
22
+ RelaxDB::View.design_doc.save
23
+
24
+ end
25
+
26
+ it "should create corresponding views" do
27
+ dd = RelaxDB::DesignDocument.get "spec_doc"
28
+ dd.data["views"]["ViewByFoo_by_foo"].should be
29
+ end
30
+
31
+ it "should create a by_ att list method" do
32
+ ViewByFoo.new(:foo => :bar).save!
33
+ res = ViewByFoo.by_foo
34
+ res.first.foo.should == "bar"
35
+ end
36
+
37
+ it "should create a paginate_by_ att list method" do
38
+ ViewByFoo.new(:foo => :bar).save!
39
+ res = ViewByFoo.paginate_by_foo :page_params => {}, :startkey => {}, :endkey => nil
40
+ res.first.foo.should == "bar"
41
+ end
42
+
43
+ it "should apply query defaults to by_" do
44
+ ViewByFoo.new(:foo => "a").save!
45
+ ViewByFoo.new(:foo => "b").save!
46
+
47
+ ViewByFoo.by_foo.map{ |o| o.foo }.should == ["b", "a"]
48
+ end
49
+
50
+ it "should return the right count size" do
51
+ docs = (1..101).map { |i| ViewByFoo.new :foo => i }
52
+ RelaxDB.bulk_save! *docs
53
+ count = RelaxDB.view "ViewByFoo_by_foo", :reduce => true
54
+ count.should == 101
55
+ end
56
+
57
+ it "should allow a single arg to be passed to by_" do
58
+ vbf = ViewByFoo.new(:foo => "a").save!
59
+ ViewByFoo.by_foo("a").should == vbf
60
+ end
61
+
62
+ it "should apply query defaults to paginate_by_" do
63
+ ViewByFoo.new(:foo => "a").save!
64
+ ViewByFoo.new(:foo => "b").save!
65
+
66
+ res = ViewByFoo.paginate_by_foo :page_params => {}, :startkey => {}, :endkey => nil
67
+ res.map{ |o| o.foo }.should == ["b", "a"]
68
+ end
69
+
70
+ it "should allow query defaults to be overridden for paginate_by_" do
71
+ ViewByFoo.new(:foo => :bar).save!
72
+ res = ViewByFoo.paginate_by_foo :page_params => {}, :startkey => nil, :endkey => {}, :descending => false
73
+ res.first.foo.should == "bar"
74
+ end
75
+
76
+ it "should allow query defaults to be overridden for by_" do
77
+ ViewByFoo.new(:foo => "a").save!
78
+ ViewByFoo.new(:foo => "b").save!
79
+
80
+ ViewByFoo.by_foo(:descending => false).map{ |o| o.foo }.should == ["a", "b"]
81
+ end
82
+
83
+ end
84
+
85
+ end
metadata CHANGED
@@ -1,7 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaxdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 5
8
+ version: "0.5"
5
9
  platform: ruby
6
10
  authors:
7
11
  - Paul Carey
@@ -9,39 +13,47 @@ autorequire: relaxdb
9
13
  bindir: bin
10
14
  cert_chain: []
11
15
 
12
- date: 2009-10-22 00:00:00 +01:00
16
+ date: 2010-04-27 00:00:00 +01:00
13
17
  default_executable:
14
18
  dependencies:
15
19
  - !ruby/object:Gem::Dependency
16
20
  name: extlib
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
20
23
  requirements:
21
24
  - - ">="
22
25
  - !ruby/object:Gem::Version
26
+ segments:
27
+ - 0
28
+ - 9
29
+ - 4
23
30
  version: 0.9.4
24
- version:
31
+ type: :runtime
32
+ version_requirements: *id001
25
33
  - !ruby/object:Gem::Dependency
26
34
  name: json
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
30
37
  requirements:
31
38
  - - ">="
32
39
  - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
33
42
  version: "0"
34
- version:
43
+ type: :runtime
44
+ version_requirements: *id002
35
45
  - !ruby/object:Gem::Dependency
36
46
  name: uuid
37
- type: :runtime
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
47
+ prerelease: false
48
+ requirement: &id003 !ruby/object:Gem::Requirement
40
49
  requirements:
41
50
  - - ">="
42
51
  - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
43
54
  version: "0"
44
- version:
55
+ type: :runtime
56
+ version_requirements: *id003
45
57
  description: RelaxDB provides a simple interface to CouchDB
46
58
  email: paul.p.carey@gmail.com
47
59
  executables: []
@@ -57,54 +69,51 @@ files:
57
69
  - readme.rb
58
70
  - Rakefile
59
71
  - docs/spec_results.html
60
- - lib/more/atomic_bulk_save_support.rb
61
72
  - lib/more/grapher.rb
62
73
  - lib/relaxdb/all_delegator.rb
63
- - lib/relaxdb/belongs_to_proxy.rb
64
74
  - lib/relaxdb/design_doc.rb
65
75
  - lib/relaxdb/document.rb
66
76
  - lib/relaxdb/extlib.rb
67
- - lib/relaxdb/has_many_proxy.rb
68
- - lib/relaxdb/has_one_proxy.rb
69
77
  - lib/relaxdb/migration.rb
70
78
  - lib/relaxdb/migration_version.rb
71
79
  - lib/relaxdb/net_http_server.rb
72
80
  - lib/relaxdb/paginate_params.rb
73
81
  - lib/relaxdb/paginator.rb
74
82
  - lib/relaxdb/query.rb
75
- - lib/relaxdb/references_many_proxy.rb
83
+ - lib/relaxdb/references_proxy.rb
76
84
  - lib/relaxdb/relaxdb.rb
77
85
  - lib/relaxdb/server.rb
78
86
  - lib/relaxdb/taf2_curb_server.rb
79
87
  - lib/relaxdb/uuid_generator.rb
80
88
  - lib/relaxdb/validators.rb
89
+ - lib/relaxdb/view_by_delegator.rb
81
90
  - lib/relaxdb/view_object.rb
82
91
  - lib/relaxdb/view_result.rb
83
92
  - lib/relaxdb/view_uploader.rb
84
93
  - lib/relaxdb/views.rb
85
94
  - lib/relaxdb.rb
86
- - spec/belongs_to_spec.rb
95
+ - spec/all_delegator_spec.rb
87
96
  - spec/callbacks_spec.rb
88
97
  - spec/derived_properties_spec.rb
89
98
  - spec/design_doc_spec.rb
90
99
  - spec/doc_inheritable_spec.rb
91
100
  - spec/document_spec.rb
92
- - spec/has_many_spec.rb
93
- - spec/has_one_spec.rb
94
101
  - spec/migration_spec.rb
95
102
  - spec/migration_version_spec.rb
96
103
  - spec/paginate_params_spec.rb
97
104
  - spec/paginate_spec.rb
105
+ - spec/qpaginate_spec.rb
98
106
  - spec/query_spec.rb
99
- - spec/references_many_spec.rb
107
+ - spec/references_proxy_spec.rb
100
108
  - spec/relaxdb_spec.rb
101
109
  - spec/server_spec.rb
102
110
  - spec/spec.opts
103
111
  - spec/spec_helper.rb
104
112
  - spec/spec_models.rb
113
+ - spec/uuid_generator_spec.rb
105
114
  - spec/view_by_spec.rb
115
+ - spec/view_docs_by_spec.rb
106
116
  - spec/view_object_spec.rb
107
- - spec/view_spec.rb
108
117
  has_rdoc: true
109
118
  homepage: http://github.com/paulcarey/relaxdb/
110
119
  licenses: []
@@ -118,18 +127,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
127
  requirements:
119
128
  - - ">="
120
129
  - !ruby/object:Gem::Version
130
+ segments:
131
+ - 0
121
132
  version: "0"
122
- version:
123
133
  required_rubygems_version: !ruby/object:Gem::Requirement
124
134
  requirements:
125
135
  - - ">="
126
136
  - !ruby/object:Gem::Version
137
+ segments:
138
+ - 0
127
139
  version: "0"
128
- version:
129
140
  requirements: []
130
141
 
131
142
  rubyforge_project:
132
- rubygems_version: 1.3.5
143
+ rubygems_version: 1.3.6
133
144
  signing_key:
134
145
  specification_version: 3
135
146
  summary: RelaxDB provides a simple interface to CouchDB
@@ -1,18 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__) + '/../lib')
2
- require 'relaxdb'
3
- require File.dirname(__FILE__) + '/../../spec/spec_models.rb'
4
-
5
- RelaxDB.configure :host => "localhost", :port => 5984
6
- RelaxDB.delete_db "relaxdb_spec" rescue :ok
7
- RelaxDB.use_db "relaxdb_spec"
8
-
9
- a1 = Atom.new.save!
10
- a1_dup = a1.dup
11
- a1.save!
12
- begin
13
- RelaxDB.bulk_save! a1_dup
14
- puts "Atomic bulk_save _not_ supported"
15
- rescue RelaxDB::UpdateConflict
16
- puts "Atomic bulk_save supported"
17
- end
18
-
@@ -1,101 +0,0 @@
1
- module RelaxDB
2
-
3
- class HasManyProxy
4
-
5
- include Enumerable
6
-
7
- attr_reader :children
8
-
9
- def initialize(client, relationship, opts)
10
- @client = client
11
- @relationship = relationship
12
- @opts = opts
13
-
14
- @target_class = opts[:class]
15
- @relationship_as_viewed_by_target = (opts[:known_as] || client.class.name.snake_case).to_s
16
-
17
- @children = load_children
18
- end
19
-
20
- def <<(obj)
21
- return false if @children.include?(obj)
22
-
23
- obj.send("#{@relationship_as_viewed_by_target}=".to_sym, @client)
24
- if obj.save
25
- @children << obj
26
- self
27
- else
28
- false
29
- end
30
- end
31
-
32
- def clear
33
- @children.each do |c|
34
- break_back_link c
35
- end
36
- @children.clear
37
- end
38
-
39
- def delete(obj)
40
- obj = @children.delete(obj)
41
- break_back_link(obj) if obj
42
- end
43
-
44
- def break_back_link(obj)
45
- if obj
46
- obj.send("#{@relationship_as_viewed_by_target}=".to_sym, nil)
47
- obj.save
48
- end
49
- end
50
-
51
- def empty?
52
- @children.empty?
53
- end
54
-
55
- def size
56
- @children.size
57
- end
58
-
59
- def [](*args)
60
- @children[*args]
61
- end
62
-
63
- def first
64
- @children[0]
65
- end
66
-
67
- def last
68
- @children[size-1]
69
- end
70
-
71
- def each(&blk)
72
- @children.each(&blk)
73
- end
74
-
75
- def reload
76
- @children = load_children
77
- end
78
-
79
- def load_children
80
- view_name = "#{@client.class}_#{@relationship}"
81
- @children = RelaxDB.view(view_name, :key => @client._id)
82
- end
83
-
84
- def children=(children)
85
- children.each do |obj|
86
- obj.send("#{@relationship_as_viewed_by_target}=".to_sym, @client)
87
- end
88
- @children = children
89
- end
90
-
91
- def inspect
92
- @children.inspect
93
- end
94
-
95
- # Play nice with Merb partials - [ obj ].flatten invokes
96
- # obj.to_ary if it responds to to_ary
97
- alias_method :to_ary, :to_a
98
-
99
- end
100
-
101
- end