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, GraphCreator, HasManyProxy, HasOneProxy, PaginateParams, Paginator, Query, ReferencesManyProxy, Server, SortedByView, UuidGenerator, ViewCreator, ViewObject, ViewResult, ViewUploader
Constant Summary
collapse
- @@db =
nil
Class Method Summary
collapse
-
.bulk_save(*objs) ⇒ Object
-
.configure(config) ⇒ Object
-
.create_from_hash(data) ⇒ Object
-
.create_object(data) ⇒ Object
-
.db ⇒ Object
-
.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
-
.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_function = 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
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/relaxdb/relaxdb.rb', line 36
def bulk_save(*objs)
docs = {}
objs.each { |o| docs[o._id] = o }
resp = db.post("_bulk_docs", { "docs" => objs }.to_json )
data = JSON.parse(resp.body)
data["new_revs"].each do |new_rev|
docs[ new_rev["id"] ]._rev = new_rev["rev"]
end
data["ok"]
end
|
7
8
9
|
# File 'lib/relaxdb/relaxdb.rb', line 7
def configure(config)
@@db = CouchDB.new(config)
end
|
.create_from_hash(data) ⇒ Object
127
128
129
|
# File 'lib/relaxdb/relaxdb.rb', line 127
def create_from_hash(data)
data["rows"].map { |row| create_object(row["value"]) }
end
|
.create_object(data) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/relaxdb/relaxdb.rb', line 131
def create_object(data)
klass = data.delete("class")
if klass
k = Module.const_get(klass)
k.new(data)
else
ViewObject.create(data)
end
end
|
.db ⇒ Object
11
12
13
|
# File 'lib/relaxdb/relaxdb.rb', line 11
def db
@@db
end
|
.delete_db(name) ⇒ Object
24
25
26
|
# File 'lib/relaxdb/relaxdb.rb', line 24
def delete_db(name)
db.delete_db(name)
end
|
.get(uri = nil) ⇒ Object
Convenience methods - should be in a diffent module?
145
146
147
|
# File 'lib/relaxdb/relaxdb.rb', line 145
def get(uri=nil)
JSON.parse(db.get(uri).body)
end
|
.instantiate(data) ⇒ Object
Creates RelaxDB::Document objects from the result
99
100
101
|
# File 'lib/relaxdb/relaxdb.rb', line 99
def instantiate(data)
create_from_hash(data)
end
|
.list_dbs ⇒ Object
28
29
30
|
# File 'lib/relaxdb/relaxdb.rb', line 28
def list_dbs
db.list_dbs
end
|
.load(*ids) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/relaxdb/relaxdb.rb', line 50
def load(*ids)
if ids.size == 1
resp = db.get(ids[0])
data = JSON.parse(resp.body)
create_object(data)
else
resp = db.post("_all_docs?include_docs=true", {:keys => ids}.to_json)
data = JSON.parse(resp.body)
data["rows"].map { |row| create_object(row["doc"]) }
end
end
|
.logger ⇒ Object
15
16
17
|
# File 'lib/relaxdb/relaxdb.rb', line 15
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
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/relaxdb/relaxdb.rb', line 87
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/relaxdb/relaxdb.rb', line 109
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
149
150
151
152
|
# File 'lib/relaxdb/relaxdb.rb', line 149
def pp_get(uri=nil)
resp = db.get(uri)
pp(JSON.parse(resp.body))
end
|
.pp_post(uri = nil, json = nil) ⇒ Object
154
155
156
157
|
# File 'lib/relaxdb/relaxdb.rb', line 154
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
104
105
106
107
|
# File 'lib/relaxdb/relaxdb.rb', line 104
def reduce_result(data)
obj = data["rows"][0] && data["rows"][0]["value"]
ViewObject.create(obj)
end
|
.replicate_db(source, target) ⇒ Object
32
33
34
|
# File 'lib/relaxdb/relaxdb.rb', line 32
def replicate_db(source, target)
db.replicate_db source, target
end
|
.retrieve(view_path, design_doc = nil, view_name = nil, map_function = nil) ⇒ Object
Used internally by RelaxDB
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/relaxdb/relaxdb.rb', line 63
def retrieve(view_path, design_doc=nil, view_name=nil, map_function=nil)
begin
resp = db.get(view_path)
rescue => e
DesignDocument.get(design_doc).add_map_view(view_name, map_function).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
20
21
22
|
# File 'lib/relaxdb/relaxdb.rb', line 20
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.
77
78
79
80
81
82
83
|
# File 'lib/relaxdb/relaxdb.rb', line 77
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
|