paulcarey-relaxdb 0.2.8 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +100 -108
- data/Rakefile +13 -2
- data/docs/spec_results.html +609 -154
- data/lib/more/atomic_bulk_save_support.rb +18 -0
- data/lib/relaxdb/all_delegator.rb +15 -39
- data/lib/relaxdb/design_doc.rb +8 -6
- data/lib/relaxdb/document.rb +140 -74
- data/lib/relaxdb/has_many_proxy.rb +6 -5
- data/lib/relaxdb/has_one_proxy.rb +2 -5
- data/lib/relaxdb/paginate_params.rb +3 -4
- data/lib/relaxdb/paginator.rb +12 -16
- data/lib/relaxdb/query.rb +17 -13
- data/lib/relaxdb/references_many_proxy.rb +3 -5
- data/lib/relaxdb/relaxdb.rb +51 -45
- data/lib/relaxdb/server.rb +4 -4
- data/lib/relaxdb/view_uploader.rb +10 -8
- data/lib/relaxdb/views.rb +88 -28
- data/lib/relaxdb.rb +0 -1
- data/readme.rb +79 -0
- data/spec/belongs_to_spec.rb +1 -6
- data/spec/callbacks_spec.rb +19 -3
- data/spec/derived_properties_spec.rb +2 -7
- data/spec/design_doc_spec.rb +3 -3
- data/spec/document_spec.rb +19 -57
- data/spec/has_many_spec.rb +11 -14
- data/spec/has_one_spec.rb +1 -6
- data/spec/query_spec.rb +26 -22
- data/spec/references_many_spec.rb +1 -6
- data/spec/relaxdb_spec.rb +88 -29
- data/spec/spec_helper.rb +34 -0
- data/spec/spec_models.rb +163 -118
- metadata +7 -3
- data/lib/relaxdb/sorted_by_view.rb +0 -65
data/spec/spec_models.rb
CHANGED
@@ -1,151 +1,196 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# The following clause ensures the tests pass, but perhaps Document should raise a warning
|
4
|
-
# if it's loaded more than once...
|
5
|
-
#
|
6
|
-
unless @spec_models_loaded
|
7
|
-
|
8
|
-
class Atom < RelaxDB::Document
|
9
|
-
end
|
1
|
+
class Atom < RelaxDB::Document
|
2
|
+
end
|
10
3
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
4
|
+
class Initiative < RelaxDB::Document
|
5
|
+
property :x
|
6
|
+
attr_reader :foo
|
7
|
+
def initialize(data)
|
8
|
+
data[:_id] = data[:x]
|
9
|
+
super data
|
10
|
+
@foo = :bar
|
19
11
|
end
|
12
|
+
end
|
20
13
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
14
|
+
class Primitives < RelaxDB::Document
|
15
|
+
|
16
|
+
property :str
|
17
|
+
property :num
|
18
|
+
property :true_bool
|
19
|
+
property :false_bool
|
20
|
+
property :created_at
|
21
|
+
property :empty
|
29
22
|
|
30
|
-
|
23
|
+
view_by :num
|
31
24
|
|
32
|
-
|
33
|
-
property :val
|
34
|
-
def val; @val + 5; end
|
35
|
-
end
|
25
|
+
end
|
36
26
|
|
37
|
-
|
38
|
-
|
39
|
-
def val=(v); @val = v - 10; end
|
40
|
-
end
|
27
|
+
class PrimitivesChild < Primitives
|
28
|
+
end
|
41
29
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
30
|
+
class BespokeReader < RelaxDB::Document
|
31
|
+
property :val
|
32
|
+
def val; @val + 5; end
|
33
|
+
end
|
48
34
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
belongs_to :sender
|
54
|
-
belongs_to :recipient
|
55
|
-
|
56
|
-
end
|
35
|
+
class BespokeWriter < RelaxDB::Document
|
36
|
+
property :val
|
37
|
+
def val=(v); @val = v - 10; end
|
38
|
+
end
|
57
39
|
|
58
|
-
|
59
|
-
|
60
|
-
property :name
|
61
|
-
belongs_to :user
|
62
|
-
|
63
|
-
end
|
40
|
+
class Invite < RelaxDB::Document
|
64
41
|
|
65
|
-
|
66
|
-
|
67
|
-
property :name, :default => "u"
|
68
|
-
property :age
|
69
|
-
|
70
|
-
has_many :items, :class => "Item"
|
71
|
-
|
72
|
-
has_many :invites_received, :class => "Invite", :known_as => :recipient
|
73
|
-
has_many :invites_sent, :class => "Invite", :known_as => :sender
|
74
|
-
|
75
|
-
end
|
42
|
+
property :message
|
76
43
|
|
77
|
-
|
44
|
+
belongs_to :sender
|
45
|
+
belongs_to :recipient
|
78
46
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
47
|
+
end
|
48
|
+
|
49
|
+
class Item < RelaxDB::Document
|
50
|
+
|
51
|
+
property :name
|
52
|
+
belongs_to :user
|
83
53
|
|
84
|
-
|
54
|
+
view_by :user_id
|
85
55
|
|
86
|
-
|
56
|
+
end
|
87
57
|
|
88
|
-
|
89
|
-
belongs_to :photo
|
58
|
+
class User < RelaxDB::Document
|
90
59
|
|
91
|
-
|
60
|
+
property :name, :default => "u"
|
61
|
+
property :age
|
92
62
|
|
93
|
-
class
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
has_one :rating
|
98
|
-
|
99
|
-
references_many :tags, :class => "Tag", :known_as => :photos
|
63
|
+
has_many :items, :class => "Item"
|
64
|
+
|
65
|
+
has_many :invites_received, :class => "Invite", :known_as => :recipient
|
66
|
+
has_many :invites_sent, :class => "Invite", :known_as => :sender
|
100
67
|
|
101
|
-
|
68
|
+
view_by :name, :age
|
102
69
|
|
103
|
-
|
70
|
+
end
|
104
71
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
72
|
+
class Post < RelaxDB::Document
|
73
|
+
|
74
|
+
property :subject
|
75
|
+
property :content
|
76
|
+
property :created_at
|
77
|
+
property :viewed_at
|
109
78
|
|
110
|
-
|
79
|
+
view_by :content
|
80
|
+
view_by :subject
|
81
|
+
view_by :viewed_at
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
class Rating < RelaxDB::Document
|
86
|
+
|
87
|
+
property :stars, :default => 5
|
88
|
+
belongs_to :photo
|
111
89
|
|
112
|
-
|
90
|
+
view_by :stars
|
113
91
|
|
114
|
-
|
92
|
+
end
|
115
93
|
|
116
|
-
|
117
|
-
belongs_to :tag
|
118
|
-
property :relevance
|
94
|
+
class Photo < RelaxDB::Document
|
119
95
|
|
120
|
-
|
96
|
+
property :name
|
121
97
|
|
122
|
-
|
123
|
-
has_one :multi_word_child
|
124
|
-
has_many :multi_word_children, :class => "MultiWordChild"
|
125
|
-
end
|
98
|
+
has_one :rating
|
126
99
|
|
127
|
-
class
|
128
|
-
belongs_to :multi_word_class
|
129
|
-
end
|
100
|
+
references_many :tags, :class => "Tag", :known_as => :photos
|
130
101
|
|
131
|
-
class
|
132
|
-
|
133
|
-
property :name
|
134
|
-
references_many :followers, :class => "User", :known_as => :leaders
|
135
|
-
references_many :leaders, :class => "User", :known_as => :followers
|
136
|
-
|
137
|
-
end
|
102
|
+
has_many :taggings, :class => "Tagging"
|
138
103
|
|
139
|
-
|
140
|
-
has_one :failure
|
141
|
-
has_many :failures, :class => "Failure"
|
142
|
-
end
|
104
|
+
end
|
143
105
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
106
|
+
class Tag < RelaxDB::Document
|
107
|
+
|
108
|
+
property :name
|
109
|
+
references_many :photos, :class => "Photo", :known_as => :tags
|
110
|
+
|
111
|
+
has_many :taggings, :class => "Tagging"
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
class Tagging < RelaxDB::Document
|
116
|
+
|
117
|
+
belongs_to :photo
|
118
|
+
belongs_to :tag
|
119
|
+
property :relevance
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
class MultiWordClass < RelaxDB::Document
|
124
|
+
has_one :multi_word_child
|
125
|
+
has_many :multi_word_children, :class => "MultiWordChild"
|
126
|
+
end
|
127
|
+
|
128
|
+
class MultiWordChild < RelaxDB::Document
|
129
|
+
belongs_to :multi_word_class
|
130
|
+
end
|
131
|
+
|
132
|
+
class TwitterUser < RelaxDB::Document
|
133
|
+
|
134
|
+
property :name
|
135
|
+
references_many :followers, :class => "User", :known_as => :leaders
|
136
|
+
references_many :leaders, :class => "User", :known_as => :followers
|
148
137
|
|
149
|
-
|
138
|
+
end
|
139
|
+
|
140
|
+
class Dysfunctional < RelaxDB::Document
|
141
|
+
has_one :failure
|
142
|
+
has_many :failures, :class => "Failure"
|
143
|
+
end
|
144
|
+
|
145
|
+
class Failure < RelaxDB::Document
|
146
|
+
property :pathological, :validator => lambda { false }
|
147
|
+
belongs_to :dysfunctional
|
148
|
+
end
|
149
|
+
|
150
|
+
class Letter < RelaxDB::Document
|
151
|
+
property :letter
|
152
|
+
property :number
|
153
|
+
view_by :letter, :number
|
154
|
+
view_by :number
|
155
|
+
end
|
156
|
+
|
157
|
+
class Ancestor < RelaxDB::Document
|
158
|
+
|
159
|
+
property :x
|
160
|
+
property :y, :default => true,
|
161
|
+
:validator => lambda { |y| y },
|
162
|
+
:validation_msg => "Uh oh"
|
163
|
+
|
164
|
+
references :user
|
165
|
+
property :user_name,
|
166
|
+
:derived => [:user, lambda { |p, o| o.user.name } ]
|
167
|
+
|
168
|
+
view_by :x
|
169
|
+
end
|
150
170
|
|
171
|
+
class Descendant < Ancestor
|
172
|
+
end
|
173
|
+
|
174
|
+
class SubDescendant < Descendant
|
175
|
+
end
|
176
|
+
|
177
|
+
class RichDescendant < Descendant
|
178
|
+
property :foo
|
179
|
+
|
180
|
+
references :ukulele
|
181
|
+
property :ukulele_name,
|
182
|
+
:derived => [:ukulele, lambda { |p, o| o.user.name } ]
|
183
|
+
end
|
184
|
+
|
185
|
+
module Inh
|
186
|
+
|
187
|
+
class X < RelaxDB::Document; end
|
188
|
+
|
189
|
+
class Y < X; end
|
190
|
+
class Y1 < Y; end
|
191
|
+
|
192
|
+
class Z < X; end
|
193
|
+
class Z1 < Z; end
|
194
|
+
class Z2 < Z; end
|
195
|
+
|
151
196
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paulcarey-relaxdb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Carey
|
@@ -9,11 +9,12 @@ autorequire: relaxdb
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-03-31 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: extlib
|
17
|
+
type: :runtime
|
17
18
|
version_requirement:
|
18
19
|
version_requirements: !ruby/object:Gem::Requirement
|
19
20
|
requirements:
|
@@ -23,6 +24,7 @@ dependencies:
|
|
23
24
|
version:
|
24
25
|
- !ruby/object:Gem::Dependency
|
25
26
|
name: json
|
27
|
+
type: :runtime
|
26
28
|
version_requirement:
|
27
29
|
version_requirements: !ruby/object:Gem::Requirement
|
28
30
|
requirements:
|
@@ -32,6 +34,7 @@ dependencies:
|
|
32
34
|
version:
|
33
35
|
- !ruby/object:Gem::Dependency
|
34
36
|
name: uuid
|
37
|
+
type: :runtime
|
35
38
|
version_requirement:
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
@@ -50,6 +53,7 @@ extra_rdoc_files: []
|
|
50
53
|
files:
|
51
54
|
- LICENSE
|
52
55
|
- README.textile
|
56
|
+
- readme.rb
|
53
57
|
- Rakefile
|
54
58
|
- docs/spec_results.html
|
55
59
|
- lib/relaxdb
|
@@ -66,7 +70,6 @@ files:
|
|
66
70
|
- lib/relaxdb/references_many_proxy.rb
|
67
71
|
- lib/relaxdb/relaxdb.rb
|
68
72
|
- lib/relaxdb/server.rb
|
69
|
-
- lib/relaxdb/sorted_by_view.rb
|
70
73
|
- lib/relaxdb/uuid_generator.rb
|
71
74
|
- lib/relaxdb/validators.rb
|
72
75
|
- lib/relaxdb/view_object.rb
|
@@ -75,6 +78,7 @@ files:
|
|
75
78
|
- lib/relaxdb/views.rb
|
76
79
|
- lib/more/grapher.rb
|
77
80
|
- lib/relaxdb.rb
|
81
|
+
- lib/more/atomic_bulk_save_support.rb
|
78
82
|
- spec/belongs_to_spec.rb
|
79
83
|
- spec/callbacks_spec.rb
|
80
84
|
- spec/design_doc_spec.rb
|
@@ -1,65 +0,0 @@
|
|
1
|
-
module RelaxDB
|
2
|
-
|
3
|
-
# Represents a CouchDB view, which is implicitly sorted by key
|
4
|
-
# The view name is determined by sort attributes
|
5
|
-
class SortedByView
|
6
|
-
|
7
|
-
def initialize(class_name, *atts)
|
8
|
-
@class_name = class_name
|
9
|
-
@atts = atts
|
10
|
-
end
|
11
|
-
|
12
|
-
def map_function
|
13
|
-
key = @atts.map { |a| "doc.#{a}" }.join(", ")
|
14
|
-
key = @atts.size > 1 ? key.sub(/^/, "[").sub(/$/, "]") : key
|
15
|
-
|
16
|
-
<<-QUERY
|
17
|
-
function(doc) {
|
18
|
-
if(doc.class == "#{@class_name}") {
|
19
|
-
emit(#{key}, doc);
|
20
|
-
}
|
21
|
-
}
|
22
|
-
QUERY
|
23
|
-
end
|
24
|
-
|
25
|
-
def reduce_function
|
26
|
-
<<-QUERY
|
27
|
-
function(keys, values, rereduce) {
|
28
|
-
if (rereduce) {
|
29
|
-
return sum(values);
|
30
|
-
} else {
|
31
|
-
return values.length;
|
32
|
-
}
|
33
|
-
}
|
34
|
-
QUERY
|
35
|
-
end
|
36
|
-
|
37
|
-
def view_name
|
38
|
-
"all_sorted_by_" << @atts.join("_and_")
|
39
|
-
end
|
40
|
-
|
41
|
-
def query(query)
|
42
|
-
# If a view contains both a map and reduce function, CouchDB will by default return
|
43
|
-
# the result of the reduce function when queried.
|
44
|
-
# This class automatically creates both map and reduce functions so it can be used by the paginator.
|
45
|
-
# In normal usage, this class will be used with map functions, hence reduce is explicitly set
|
46
|
-
# to false if it hasn't already been set.
|
47
|
-
query.reduce(false) if query.reduce.nil?
|
48
|
-
|
49
|
-
method = query.keys ? :post : :get
|
50
|
-
|
51
|
-
begin
|
52
|
-
resp = RelaxDB.db.send(method, query.view_path, query.keys)
|
53
|
-
rescue => e
|
54
|
-
design_doc = DesignDocument.get(@class_name)
|
55
|
-
design_doc.add_map_view(view_name, map_function).add_reduce_view(view_name, reduce_function).save
|
56
|
-
resp = RelaxDB.db.send(method, query.view_path, query.keys)
|
57
|
-
end
|
58
|
-
|
59
|
-
data = JSON.parse(resp.body)
|
60
|
-
ViewResult.new(data)
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|