leanback 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.rdoc +14 -1
- data/README.rdoc +49 -36
- data/VERSION +1 -1
- data/leanback.gemspec +1 -1
- data/lib/leanback.rb +26 -0
- data/test/test_leanback.rb +25 -3
- metadata +3 -3
data/Changelog.rdoc
CHANGED
@@ -1,10 +1,23 @@
|
|
1
|
+
=Leanback 0.2.7
|
2
|
+
August 4, 2011:-
|
3
|
+
* Added ability to Query a permanent view and create it on the fly from a json file, if it doesn't already exist and then return the values
|
4
|
+
Example:
|
5
|
+
view = {:database => 'contacts',
|
6
|
+
:design_doc => 'my_views',
|
7
|
+
:view => 'get_emails',
|
8
|
+
:json_doc => '/path/to/my_views.json'}
|
9
|
+
|
10
|
+
Couchdb.find_on_fly(view)
|
11
|
+
The above will query the view and add it to the database if it doesn't already exist and still return the values.
|
12
|
+
See README for details
|
13
|
+
|
1
14
|
=Leanback 0.2.6
|
2
15
|
August 3, 2011:-
|
3
16
|
* Added ability to delete documents without having to retrieve the _rev revision number
|
4
17
|
Example:
|
5
18
|
doc = {:database => 'contacts', :doc_id => 'James'}
|
6
19
|
Document.delete doc
|
7
|
-
The above will delete the document with id '
|
20
|
+
The above will delete the document with id 'James'. See README for details.
|
8
21
|
|
9
22
|
=Leanback 0.2.5
|
10
23
|
August 2, 2011:-
|
data/README.rdoc
CHANGED
@@ -250,39 +250,34 @@ To query the view
|
|
250
250
|
{"Firstname"=>"john", "Lastname"=>"smith", "Email"=>"john@mail.com"}]
|
251
251
|
|
252
252
|
|
253
|
-
===
|
253
|
+
===Query a View and Create it on the fly if it doesn't already exist
|
254
254
|
Let's say we want to query a view, and create it on the fly if it doesn't already exist, and still return the values. In this example we will query a view called get_emails view (which returns emails of all contacts), if this view doesn't already exist it will be added to the database.
|
255
255
|
|
256
|
+
|
256
257
|
def get_emails
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
258
|
+
view = {:database => 'contacts',
|
259
|
+
:design_doc => 'my_views',
|
260
|
+
:view => 'get_emails',
|
261
|
+
:json_doc => '/path/to/my_views.json'}
|
262
|
+
|
263
|
+
Couchdb.find_on_fly(view)
|
263
264
|
end
|
264
|
-
return docs
|
265
|
-
end
|
266
265
|
|
267
|
-
|
268
|
-
|
266
|
+
email_list = get_emails()
|
267
|
+
puts email_list.inspect
|
269
268
|
|
270
269
|
# => [{"Name"=>"Nancy", "Email"=>"nancy@mail.com"}, {"Name"=>"John", "Email"=>"john@mail.com"}]
|
271
270
|
|
272
271
|
In get_emails(),
|
273
|
-
|
272
|
+
Couchdb.find_on_fly(view)
|
274
273
|
sends a request to the design_document (my_views) and view (get_emails). If this view is found in the database, it returns an Array with the name and email of all contacts:
|
275
274
|
[{"Name"=>"Nancy", "Email"=>"nancy@mail.com"}, {"Name"=>"John", "Email"=>"john@mail.com"}]
|
276
|
-
if the view doesn't exist it
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
docs = Couchdb.find(:database => "contacts", :design_doc => 'my_views', :view => 'get_emails')
|
283
|
-
Then it returns the email list
|
284
|
-
return docs
|
285
|
-
So the first time the get_emails() method is called, the view will be created on the fly and added to the database.
|
275
|
+
if the design document and view doesn't already exist, it creates it and adds it to the database using the json document
|
276
|
+
:json_doc => '/path/to/my_views.json'
|
277
|
+
|
278
|
+
next it sends a request for the view again and then it returns the email list.
|
279
|
+
|
280
|
+
So the first time the get_emails() method is called, the view will be created on the fly and added to the database. Future calls to get_email would simply return the values from the view.
|
286
281
|
|
287
282
|
Source for the get_emails view:
|
288
283
|
#my_views.json
|
@@ -298,6 +293,37 @@ Source for the get_emails view:
|
|
298
293
|
}
|
299
294
|
}
|
300
295
|
|
296
|
+
You can also query a permanent view with a key and create it on the fly, if it doesn't already exist.
|
297
|
+
Let's say we want to view all contacts with age = "36", and we already defined the view in a json file (view_age.json).
|
298
|
+
view = { :database => 'contacts',
|
299
|
+
:design_doc => 'the_view',
|
300
|
+
:view => 'age',
|
301
|
+
:json_doc => '/path/to/view_age.json'}
|
302
|
+
|
303
|
+
age = '36'
|
304
|
+
Couchdb.find_on_fly(view,key = age)
|
305
|
+
|
306
|
+
#=> [{"_id"=>"Nancy", "_rev"=>"2-4404d0a5a1a3dff103fd46faf1e46c30", "firstname"=>"Nancy", "lastname"=>"Lee",
|
307
|
+
"phone"=>"347-808-3734", "email"=>"nancy@mail.com", "gender"=>"female", "age"=>"36"}]
|
308
|
+
|
309
|
+
The above example will query the design_document (the_view) and view (age) in the database (contacts), using the key (age). If the view doesn't exist it will be created from the json document (view.age.json). And then return the values from the view.
|
310
|
+
|
311
|
+
This is similar to sending a
|
312
|
+
GET http://127.0.0.1:5984/contacts/_design/the_view/_view/age?key="36"
|
313
|
+
|
314
|
+
The view is generated from the source code:
|
315
|
+
#view_age.json
|
316
|
+
{
|
317
|
+
"language" : "javascript",
|
318
|
+
"views" :{
|
319
|
+
"age" : {
|
320
|
+
"map" : "function(doc){
|
321
|
+
if(doc.age)
|
322
|
+
emit(doc.age,doc);
|
323
|
+
}"
|
324
|
+
}
|
325
|
+
}
|
326
|
+
}
|
301
327
|
|
302
328
|
===Error handling
|
303
329
|
Every database operation raises a CouchdbException when things go wrong.
|
@@ -338,21 +364,8 @@ Attempting to query a permanent view that doesn't exist
|
|
338
364
|
# => Error message: CouchDB: Error - not_found. Reason - missing_named_view
|
339
365
|
Error value: not_found
|
340
366
|
|
341
|
-
You can always check the information passed to the Exception.
|
342
|
-
begin
|
343
|
-
view = { :database => "contacts", :design_doc => 'more_views', :view => 'get_user_email'}
|
344
|
-
Couchdb.find view
|
345
|
-
rescue CouchdbException => e
|
346
|
-
if e.error == "not_found"
|
347
|
-
doc = { :database => 'contacts', :design_doc => 'more_views', :json_doc => '/path/to/user_email.json' }
|
348
|
-
Couchdb.create_design doc
|
349
|
-
docs = Couchdb.find(:database => "contacts", :design_doc => 'more_views', :view => 'get_user_email')
|
350
|
-
end
|
351
|
-
end
|
352
|
-
|
353
|
-
In the code above, the rescue block will create and request the permanent view if it's not found by the initial request outside the block. e.error is the value of the error returned by CouchDB.
|
367
|
+
You can always check the information passed to the Exception.
|
354
368
|
|
355
|
-
|
356
369
|
===Bind Address
|
357
370
|
Leanback uses the default Couchdb bind address http://127.0.0.1:5984. To use a different bind address;
|
358
371
|
Couchdb.address = '192.168.2.16'
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.7
|
data/leanback.gemspec
CHANGED
data/lib/leanback.rb
CHANGED
@@ -196,6 +196,32 @@ def self.create_design(doc)
|
|
196
196
|
end
|
197
197
|
end
|
198
198
|
|
199
|
+
#Query view, create view on fly if it dosen't already exist
|
200
|
+
def self.find_on_fly(doc, key = nil)
|
201
|
+
db_name = doc[:database]
|
202
|
+
design_doc = doc[:design_doc]
|
203
|
+
view = doc[:view]
|
204
|
+
json_doc = doc[:json_doc]
|
205
|
+
|
206
|
+
begin
|
207
|
+
if( key == nil)
|
208
|
+
docs = find(:database => db_name, :design_doc => design_doc, :view => view)
|
209
|
+
else
|
210
|
+
docs = find({:database => db_name, :design_doc => design_doc, :view => view},key)
|
211
|
+
end
|
212
|
+
rescue CouchdbException => e
|
213
|
+
document = { :database => db_name, :design_doc => design_doc, :json_doc => json_doc}
|
214
|
+
create_design document
|
215
|
+
if( key == nil)
|
216
|
+
docs = find(:database => db_name, :design_doc => design_doc, :view => view)
|
217
|
+
else
|
218
|
+
docs = find({:database => db_name, :design_doc => design_doc, :view => view},key)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
return docs
|
222
|
+
end
|
223
|
+
|
224
|
+
|
199
225
|
#add a finder method to the database
|
200
226
|
#this creates a find by key method
|
201
227
|
def self.add_finder(options)
|
data/test/test_leanback.rb
CHANGED
@@ -82,6 +82,28 @@ class TestLeanback < Test::Unit::TestCase
|
|
82
82
|
#puts hash.inspect
|
83
83
|
end
|
84
84
|
|
85
|
+
|
86
|
+
should "Query a permanent view and create the view on the fly, if it doesn't already exist" do
|
87
|
+
view = {:database => 'contacts',
|
88
|
+
:design_doc => 'my_views',
|
89
|
+
:view => 'get_emails',
|
90
|
+
:json_doc => '/home/obi/bin/my_views.json'}
|
91
|
+
|
92
|
+
hash = Couchdb.find_on_fly(view)
|
93
|
+
#puts hash.inspect
|
94
|
+
end
|
95
|
+
|
96
|
+
should "Query a permanent view by key and create the view on the fly, if it doesn't already exist" do
|
97
|
+
view = { :database => 'contacts',
|
98
|
+
:design_doc => 'the_view',
|
99
|
+
:view => 'age',
|
100
|
+
:json_doc => '/home/obi/bin/view_age.json'}
|
101
|
+
|
102
|
+
age = '36'
|
103
|
+
hash = Couchdb.find_on_fly(view,key = age)
|
104
|
+
puts hash.inspect
|
105
|
+
end
|
106
|
+
|
85
107
|
should "Create a design doc/permanent view or handle exception" do
|
86
108
|
doc = { :database => 'contacts', :design_doc => 'more_views', :json_doc => '/home/obi/bin/leanback/test/my_views.json' }
|
87
109
|
begin
|
@@ -119,7 +141,7 @@ class TestLeanback < Test::Unit::TestCase
|
|
119
141
|
|
120
142
|
should "update the document" do
|
121
143
|
#data = {"age" => "42", "lastname" => "arnold", "phone" => "202-456-1234", "hobbies" => "football,running, video gamess" }
|
122
|
-
data = {
|
144
|
+
data = {:age => "41", :lastname => "Stevens" }
|
123
145
|
doc = { :database => 'contacts', :doc_id => 'john', :data => data}
|
124
146
|
Document.update doc
|
125
147
|
end
|
@@ -145,11 +167,11 @@ class TestLeanback < Test::Unit::TestCase
|
|
145
167
|
:email =>'james@mail.com'}
|
146
168
|
|
147
169
|
doc = {:database => 'contacts', :doc_id => 'james', :data => data}
|
148
|
-
Document.create doc
|
170
|
+
#Document.create doc
|
149
171
|
|
150
172
|
doc = {:database => 'contacts', :doc_id => 'James'}
|
151
173
|
hash = Document.delete doc
|
152
|
-
puts hash.inspect
|
174
|
+
#puts hash.inspect
|
153
175
|
rescue CouchdbException => e
|
154
176
|
#puts e.to_s
|
155
177
|
#puts e.error
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 7
|
9
|
+
version: 0.2.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Obi Akubue
|
@@ -136,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
136
136
|
requirements:
|
137
137
|
- - ">="
|
138
138
|
- !ruby/object:Gem::Version
|
139
|
-
hash: -
|
139
|
+
hash: -441250723
|
140
140
|
segments:
|
141
141
|
- 0
|
142
142
|
version: "0"
|