Module: RelaxDB
- Defined in:
- lib/relaxdb.rb,
lib/more/grapher.rb,
lib/relaxdb/query.rb,
lib/relaxdb/views.rb,
lib/relaxdb/server.rb,
lib/relaxdb/relaxdb.rb,
lib/relaxdb/document.rb,
lib/relaxdb/paginator.rb,
lib/relaxdb/design_doc.rb,
lib/relaxdb/view_object.rb,
lib/relaxdb/view_result.rb,
lib/relaxdb/all_delegator.rb,
lib/relaxdb/has_one_proxy.rb,
lib/relaxdb/view_uploader.rb,
lib/relaxdb/has_many_proxy.rb,
lib/relaxdb/sorted_by_view.rb,
lib/relaxdb/uuid_generator.rb,
lib/relaxdb/paginate_params.rb,
lib/relaxdb/belongs_to_proxy.rb,
lib/relaxdb/references_many_proxy.rb
Defined Under Namespace
Classes: AllDelegator, BelongsToProxy, CouchDB, DesignDocument, Document, DocumentNotSaved, GraphCreator, HTTP_404, HTTP_409, HTTP_412, HasManyProxy, HasOneProxy, NotFound, PaginateParams, Paginator, Query, ReferencesManyProxy, Server, SortedByView, UpdateConflict, UuidGenerator, ValidationFailure, ViewCreator, ViewObject, ViewResult, ViewUploader
Constant Summary
collapse
- @@db =
nil
Class Method Summary
collapse
-
.bulk_save(*objs) ⇒ Object
-
.bulk_save!(*objs) ⇒ Object
-
.configure(config) ⇒ Object
-
.create_from_hash(data) ⇒ Object
-
.create_object(data) ⇒ Object
-
.db ⇒ Object
-
.db_exists?(name) ⇒ Boolean
-
.delete_db(name) ⇒ Object
-
.get(uri = nil) ⇒ Object
Convenience methods - should be in a diffent module?.
-
.instantiate(data) ⇒ Object
Creates RelaxDB::Document objects from the result.
-
.list_dbs ⇒ Object
-
.load(ids) ⇒ Object
-
.load!(ids) ⇒ Object
-
.logger ⇒ Object
-
.merge(data, merge_key) ⇒ Object
Should be invoked on the result of a join view Merges all rows based on merge_key and returns an array of ViewOject.
-
.paginate_view(page_params, design_doc, view_name, *view_keys) {|paginate_params| ... } ⇒ Object
-
.pp_get(uri = nil) ⇒ Object
-
.pp_post(uri = nil, json = nil) ⇒ Object
-
.reduce_result(data) ⇒ Object
Returns a scalar, an object, or an Array of objects.
-
.replicate_db(source, target) ⇒ Object
-
.retrieve(view_path, design_doc = nil, view_name = nil, map_func = nil, reduce_func = nil) ⇒ Object
Used internally by RelaxDB.
-
.use_db(name) ⇒ Object
Creates the named database if it doesn’t already exist.
-
.view(design_doc, view_name) {|q| ... } ⇒ Object
Requests the given view from CouchDB and returns a hash.
Class Method Details
.bulk_save(*objs) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/relaxdb/relaxdb.rb', line 68
def bulk_save(*objs)
begin
bulk_save!(*objs)
rescue ValidationFailure, UpdateConflict
false
end
end
|
.bulk_save!(*objs) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/relaxdb/relaxdb.rb', line 45
def bulk_save!(*objs)
pre_save_success = objs.inject(true) { |s, o| s &= o.pre_save }
raise ValidationFailure, objs unless pre_save_success
docs = {}
objs.each { |o| docs[o._id] = o }
begin
resp = db.post("_bulk_docs", { "docs" => objs }.to_json )
data = JSON.parse(resp.body)
data["new_revs"].each do |new_rev|
obj = docs[ new_rev["id"] ]
obj._rev = new_rev["rev"]
obj.post_save
end
rescue HTTP_412
raise UpdateConflict, objs
end
objs
end
|
12
13
14
|
# File 'lib/relaxdb/relaxdb.rb', line 12
def configure(config)
@@db = CouchDB.new(config)
end
|
.create_from_hash(data) ⇒ Object
168
169
170
|
# File 'lib/relaxdb/relaxdb.rb', line 168
def create_from_hash(data)
data["rows"].map { |row| create_object(row["value"]) }
end
|
.create_object(data) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/relaxdb/relaxdb.rb', line 172
def create_object(data)
klass = data.delete("class")
if klass
k = Module.const_get(klass)
k.new(false, data)
else
ViewObject.create(data)
end
end
|
.db ⇒ Object
16
17
18
|
# File 'lib/relaxdb/relaxdb.rb', line 16
def db
@@db
end
|
.db_exists?(name) ⇒ Boolean
29
30
31
|
# File 'lib/relaxdb/relaxdb.rb', line 29
def db_exists?(name)
db.db_exists? name
end
|
.delete_db(name) ⇒ Object
33
34
35
|
# File 'lib/relaxdb/relaxdb.rb', line 33
def delete_db(name)
db.delete_db name
end
|
.get(uri = nil) ⇒ Object
Convenience methods - should be in a diffent module?
186
187
188
|
# File 'lib/relaxdb/relaxdb.rb', line 186
def get(uri=nil)
JSON.parse(db.get(uri).body)
end
|
.instantiate(data) ⇒ Object
Creates RelaxDB::Document objects from the result
140
141
142
|
# File 'lib/relaxdb/relaxdb.rb', line 140
def instantiate(data)
create_from_hash(data)
end
|
.list_dbs ⇒ Object
37
38
39
|
# File 'lib/relaxdb/relaxdb.rb', line 37
def list_dbs
db.list_dbs
end
|
.load(ids) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/relaxdb/relaxdb.rb', line 76
def load(ids)
if ids.is_a? Array
resp = db.post("_all_docs?include_docs=true", {:keys => ids}.to_json)
data = JSON.parse(resp.body)
data["rows"].map { |row| row["doc"] ? create_object(row["doc"]) : nil }
else
begin
resp = db.get(ids)
data = JSON.parse(resp.body)
create_object(data)
rescue HTTP_404
nil
end
end
end
|
.load!(ids) ⇒ Object
92
93
94
95
96
97
98
99
|
# File 'lib/relaxdb/relaxdb.rb', line 92
def load!(ids)
res = load(ids)
raise NotFound, ids if res == nil
raise NotFound, ids if res.respond_to?(:include?) && res.include?(nil)
res
end
|
.logger ⇒ Object
20
21
22
|
# File 'lib/relaxdb/relaxdb.rb', line 20
def logger
@@db.logger
end
|
.merge(data, merge_key) ⇒ Object
Should be invoked on the result of a join view Merges all rows based on merge_key and returns an array of ViewOject
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/relaxdb/relaxdb.rb', line 128
def merge(data, merge_key)
merged = {}
data["rows"].each do |row|
value = row["value"]
merged[value[merge_key]] ||= {}
merged[value[merge_key]].merge!(value)
end
merged.values.map { |v| ViewObject.create(v) }
end
|
.paginate_view(page_params, design_doc, view_name, *view_keys) {|paginate_params| ... } ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/relaxdb/relaxdb.rb', line 150
def paginate_view(page_params, design_doc, view_name, *view_keys)
paginate_params = PaginateParams.new
yield paginate_params
raise paginate_params.error_msg if paginate_params.invalid?
paginator = Paginator.new(paginate_params, page_params)
query = Query.new(design_doc, view_name)
query.merge(paginate_params)
docs = ViewResult.new(JSON.parse(db.get(query.view_path).body))
docs.reverse! if paginate_params.order_inverted?
paginator.add_next_and_prev(docs, design_doc, view_name, view_keys)
docs
end
|
.pp_get(uri = nil) ⇒ Object
190
191
192
193
|
# File 'lib/relaxdb/relaxdb.rb', line 190
def pp_get(uri=nil)
resp = db.get(uri)
pp(JSON.parse(resp.body))
end
|
.pp_post(uri = nil, json = nil) ⇒ Object
195
196
197
198
|
# File 'lib/relaxdb/relaxdb.rb', line 195
def pp_post(uri=nil, json=nil)
resp = db.post(uri, json)
pp(JSON.parse(resp.body))
end
|
.reduce_result(data) ⇒ Object
Returns a scalar, an object, or an Array of objects
145
146
147
148
|
# File 'lib/relaxdb/relaxdb.rb', line 145
def reduce_result(data)
obj = data["rows"][0] && data["rows"][0]["value"]
ViewObject.create(obj)
end
|
.replicate_db(source, target) ⇒ Object
41
42
43
|
# File 'lib/relaxdb/relaxdb.rb', line 41
def replicate_db(source, target)
db.replicate_db source, target
end
|
.retrieve(view_path, design_doc = nil, view_name = nil, map_func = nil, reduce_func = nil) ⇒ Object
Used internally by RelaxDB
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/relaxdb/relaxdb.rb', line 102
def retrieve(view_path, design_doc=nil, view_name=nil, map_func=nil, reduce_func=nil)
begin
resp = db.get(view_path)
rescue => e
dd = DesignDocument.get(design_doc).add_map_view(view_name, map_func)
dd.add_reduce_view(view_name, reduce_func) if reduce_func
dd.save
resp = db.get(view_path)
end
data = JSON.parse(resp.body)
ViewResult.new(data)
end
|
.use_db(name) ⇒ Object
Creates the named database if it doesn’t already exist
25
26
27
|
# File 'lib/relaxdb/relaxdb.rb', line 25
def use_db(name)
db.use_db name
end
|
.view(design_doc, view_name) {|q| ... } ⇒ Object
Requests the given view from CouchDB and returns a hash. This method should typically be wrapped in one of merge, instantiate, or reduce_result.
118
119
120
121
122
123
124
|
# File 'lib/relaxdb/relaxdb.rb', line 118
def view(design_doc, view_name)
q = Query.new(design_doc, view_name)
yield q if block_given?
resp = q.keys ? db.post(q.view_path, q.keys) : db.get(q.view_path)
JSON.parse(resp.body)
end
|