leanback 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Changelog.rdoc CHANGED
@@ -1,13 +1,26 @@
1
+ =Leanback 0.2.5
2
+ August 2, 2011:-
3
+ * Added dynamic updates for documents. This makes it easy to update documents without deleting old fields or having to track the _rev value.
4
+ To update a document now:
5
+ data = {"age" => "53" }
6
+ doc = { :database => 'contacts', :doc_id => 'john', :data => data}
7
+ Document.update doc
8
+ This will update the document and change the John's age to 53. See README for details
9
+
10
+ =Leanback 0.2.4
11
+ July 20, 2011:-
12
+ * find_by(database,key) has a bug in version 0.2.3, please use version 0.2.4 instead
13
+
1
14
  =Leanback 0.2.3
2
15
  July 19, 2011:-
3
16
  * Added better error handling all database operations now raise a CouchdbException when something goes wrong. It's now easy to track exceptions.
17
+
4
18
  begin
5
19
  Couchdb.create 'contacts'
6
20
  rescue => e
7
21
  puts "Error message: " + e.to_s
8
22
  puts "Error value: " + e.error
9
23
  end
10
-
11
24
  # => Error message: CouchDB: Error - file_exists. Reason - The database could not be created, the file already exists.
12
25
  Error value: file_exists
13
26
 
@@ -21,3 +34,4 @@ The method:
21
34
  Couchdb.find_by( :database => 'contacts', :email => 'nancy@mail.com')
22
35
  will add the finder to the database if one doesn't already exist. No need to call
23
36
  Couchdb.add_finder(:database => 'contacts', :key => 'email')
37
+
data/README.rdoc CHANGED
@@ -22,14 +22,17 @@ Require the leanback Gem
22
22
 
23
23
  Create a CouchDB database
24
24
  Couchdb.create 'contacts'
25
+
25
26
  # => {"ok"=>true}
26
27
 
27
28
  Delete a database
28
29
  Couchdb.delete 'contacts'
30
+
29
31
  # => {"ok"=>true}
30
32
 
31
33
  Return a list of all Databases
32
34
  Couchdb.all
35
+
33
36
  # => ["maps", "inventory", "monitors", "contacts", "books"]
34
37
 
35
38
  Create a new document
@@ -40,30 +43,68 @@ Create a new document
40
43
 
41
44
  doc = {:database => 'contacts', :doc_id => 'Linda', :data => data}
42
45
  Document.create doc
46
+
43
47
  # => {"ok"=>true, "id"=>"Linda", "rev"=>"1-6f16274513f51e922ff1f745452a92b6"}
44
48
  This will create a new document in the 'contacts' database, with document id 'Linda'.
45
49
 
46
- Lets edit that document to change the email address, phone number, and add a gender
50
+ Let's update that document to change the email address
51
+ data = {"email" => "linda@mail.com" }
52
+ doc = { :database => 'contacts', :doc_id => 'Linda', :data => data}
53
+ Document.update doc
54
+
55
+ Let's add gender and age
56
+ data = {"age" => "32","gender" => "female" }
57
+ doc = { :database => 'contacts', :doc_id => 'Linda', :data => data}
58
+ Document.update doc
59
+
60
+
61
+ To change phone# and add a fax number
62
+ data = {"phone" => "718-234-2904","fax" => "646-309-4049" }
63
+ doc = { :database => 'contacts', :doc_id => 'Linda', :data => data}
64
+ Document.update doc
65
+
66
+ Let's add twitter account, facebook account and website url
67
+ data = {"twitter" => "http://twitter.com/#!/linda",
68
+ "faceboook" => "http://facebook.com/linda",
69
+ "website" => "http://linda-blogs.com" }
70
+
71
+ doc = { :database => 'contacts', :doc_id => 'Linda', :data => data}
72
+ Document.update doc
73
+
74
+ Retrieve/view the document
75
+ doc = {:database => 'contacts', :doc_id => 'Linda'}
76
+ Couchdb.view doc
77
+
78
+ #=> {"_id"=>"Linda", "_rev"=>"5-f99fd63f2c784b5e2f7b7d92b2df9a1e", "firstname"=>"Linda", "lastname"=>"smith",
79
+ "phone"=>"718-234-2904", "email"=>"linda@mail.com", "age"=>"32", "gender"=>"female",
80
+ "fax"=>"646-309-4049", "twitter"=>"http://twitter.com/#!/linda",
81
+ "faceboook"=>"http://facebook.com/linda", "website"=>"http://linda-blogs.com"}
82
+
83
+ To edit the document and replace it with a new one
47
84
  data = {:firstname => 'Linda',
48
85
  :lastname =>'smith',
49
86
  :email => 'linda@mail.com',
50
87
  :gender=>'female',
51
88
  :phone =>'718-245-5611',
52
- :_rev=>'1-6f16274513f51e922ff1f745452a92b6'}
89
+ :_rev=>'5-f99fd63f2c784b5e2f7b7d92b2df9a1e'}
53
90
 
54
91
  doc = {:database => 'contacts', :doc_id => 'Linda', :data => data}
55
92
  Document.edit doc
56
- # => {"ok"=>true, "id"=>"Linda", "rev"=>"2-e813a0e902e3ac114400ff3959a2adde"}
93
+
94
+ # => {"ok"=>true, "id"=>"Linda", "rev"=>"6-211ebb68bdd4ba8799387214b4a3b445"}
95
+ This replaces the existing document with a new one, the _rev property must be included in the data.
57
96
 
58
97
  Retrieve/view the document
59
98
  doc = {:database => 'contacts', :doc_id => 'Linda'}
60
99
  Couchdb.view doc
61
- #=> {"_id"=>"Linda", "_rev"=>"2-e813a0e902e3ac114400ff3959a2adde", "firstname"=>"Linda",
100
+
101
+ #=> {"_id"=>"Linda", "_rev"=>"6-211ebb68bdd4ba8799387214b4a3b445", "firstname"=>"Linda",
62
102
  "lastname"=>"smith", "email"=>"linda@mail.com", "gender"=>"female", "phone"=>"718-245-5611"}
63
103
 
64
104
  Delete the document
65
105
  doc = {:database => 'contacts', :doc_id => 'Linda', :rev => '2-e813a0e902e3ac114400ff3959a2adde'}
66
106
  Document.delete doc
107
+
67
108
  # => {"ok"=>true, "id"=>"Linda", "rev"=>"3-48580d1806983b32cb03f114efb064e3"}
68
109
 
69
110
  Retrieve all documents in a database
@@ -81,16 +122,19 @@ Retrieve all documents in a database
81
122
  ===To Find documents by key
82
123
  To find documents by email key (example: to find all contacts with email = 'nancy@mail.com')
83
124
  Couchdb.find_by( :database => 'contacts', :email => 'nancy@mail.com')
125
+
84
126
  # => [{"_id"=>"Nancy", "_rev"=>"1-d15a83d2a23b495c19df2595b636ecc8", "firstname"=>"Nancy", "lastname"=>"Lee",
85
127
  "phone"=>"347-808-3734", "email"=>"nancy@mail.com", "gender"=>"female"}]
86
128
 
87
129
  To find all contacts with lastname = 'Smith'
88
130
  Couchdb.find_by( :database => 'contacts', :lastname => 'Smith')
131
+
89
132
  # => [{"_id"=>"john", "_rev"=>"5-642689e0a50843d6fa508159a01b4fd4", "firstname"=>"John", "lastname"=>"Smith",
90
133
  "email"=>"john@mail.com", "gender"=>"male"}]
91
134
 
92
135
  To find all female contacts
93
136
  Couchdb.find_by( :database => 'contacts', :gender => 'female')
137
+
94
138
  # => [{"_id"=>"Nancy", "_rev"=>"1-d15a83d2a23b495c19df2595b636ecc8", "firstname"=>"Nancy", "lastname"=>"Lee",
95
139
  "phone"=>"347-808-3734", "email"=>"nancy@mail.com", "gender"=>"female"}]
96
140
 
@@ -144,6 +188,7 @@ You can do
144
188
  ===Query a permanent view
145
189
  view = { :database => "contacts", :design_doc => 'my_views', :view => 'get_female_contacts'}
146
190
  Couchdb.find view
191
+
147
192
  # => [{"_id"=>"Mary", "_rev"=>"5-bfcd67fd17dbb6a875af8f6dc497b15f", "firstname"=>"Mary", "lastname"=>"smith",
148
193
  "phone"=>"212-234-1234", "email"=>"mary@mail.com", "gender"=>"female"},
149
194
  {"_id"=>"Nancy", "_rev"=>"1-d15a83d2a23b495c19df2595b636ecc8", "firstname"=>"Nancy", "lastname"=>"Lee",
@@ -155,6 +200,21 @@ For the above example
155
200
  GET http://127.0.0.1:5984/contacts/_design/my_views/_view/get_female_contacts
156
201
  Leanback parses the native JSON results to return only the data values.
157
202
 
203
+ ===Query a view by key
204
+ view = { :database => "contacts", :design_doc => 'the_view', :view => 'age'}
205
+ age = "36"
206
+ Couchdb.find(view,key = age)
207
+
208
+ # => [{"_id"=>"Nancy", "_rev"=>"2-4404d0a5a1a3dff103fd46faf1e46c30", "firstname"=>"Nancy", "lastname"=>"Lee",
209
+ "phone"=>"347-808-3734", "email"=>"nancy@mail.com", "gender"=>"female", "age"=>"36"}]
210
+
211
+ This is similar to sending a
212
+ GET http://127.0.0.1:5984/[database]/_design/[design_doc]/_view/[view_name]?key="searchterm"
213
+ For the above example
214
+ GET http://127.0.0.1:5984/contacts/_design/the_view/_view/age?key="36"
215
+ Leanback parses the native JSON results to return only the data values.
216
+
217
+
158
218
  ===Create a design document with permanent views:
159
219
  First define the views in a JSON file
160
220
  //my_views.json
@@ -173,17 +233,19 @@ First define the views in a JSON file
173
233
  Now create the design document and add the json file
174
234
  doc = { :database => 'contacts', :design_doc => 'more_views', :json_doc => '/path/to/my_views.json' }
175
235
  Couchdb.create_design doc
236
+
176
237
  # => {"ok"=>true, "id"=>"_design/more_views", "rev"=>"1-d67ae97ff03a98f68ddc300bf9ae5048"}
177
238
 
178
239
  To query the view
179
240
  view = { :database => "contacts", :design_doc => 'more_views', :view => 'get_email'}
180
241
  Couchdb.find view
242
+
181
243
  # => [{"Firstname"=>"Nancy", "Lastname"=>"Lee", "Email"=>"nancy@mail.com"},
182
244
  {"Firstname"=>"john", "Lastname"=>"smith", "Email"=>"john@mail.com"}]
183
245
 
184
246
 
185
247
  ===Create View on the fly if it doesn't already exist
186
- 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.
248
+ 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.
187
249
 
188
250
  def get_emails
189
251
  begin
@@ -198,6 +260,7 @@ Let's say we want to query a view and create it on the fly if it doesn't already
198
260
 
199
261
  email_list = get_emails()
200
262
  puts email_list.inspect
263
+
201
264
  # => [{"Name"=>"Nancy", "Email"=>"nancy@mail.com"}, {"Name"=>"John", "Email"=>"john@mail.com"}]
202
265
 
203
266
  In get_emails(),
@@ -236,6 +299,7 @@ Every database operation raises a CouchdbException when things go wrong.
236
299
  Attempting to create a database that already exists
237
300
  begin
238
301
  Couchdb.create 'contacts'
302
+ #create static views
239
303
  rescue CouchdbException => e
240
304
  puts "Error message: " + e.to_s
241
305
  puts "Error value: " + e.error
@@ -305,3 +369,4 @@ To change it back to default bind address at anytime, simply set the values to n
305
369
  Copyright (c) 2011 Obi Akubue. See LICENSE.txt for
306
370
  further details.
307
371
 
372
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
1
+ 0.2.5
data/leanback.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{leanback}
8
- s.version = "0.2.4"
8
+ s.version = "0.2.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Obi Akubue"]
12
- s.date = %q{2011-07-20}
12
+ s.date = %q{2011-08-03}
13
13
  s.description = %q{lightweight Ruby interface to CouchDB}
14
14
  s.email = %q{obioraakubue@yahoo.com}
15
15
  s.extra_rdoc_files = [
data/lib/leanback.rb CHANGED
@@ -44,6 +44,18 @@ module Document
44
44
  end
45
45
  end
46
46
 
47
+ #update a doc
48
+ def self.update (doc)
49
+ db_name = doc[:database]
50
+ doc_id = doc[:doc_id]
51
+ data = doc[:data]
52
+ doc = {:database => db_name, :doc_id => doc_id}
53
+ options = Couchdb.view doc
54
+ options = options.merge(data)
55
+ doc = {:database => db_name, :doc_id => doc_id, :data => options}
56
+ edit doc
57
+ end
58
+
47
59
  #delete a doc
48
60
  def self.delete(doc)
49
61
  db_name = doc[:database]
@@ -117,6 +117,13 @@ class TestLeanback < Test::Unit::TestCase
117
117
  end
118
118
  end
119
119
 
120
+ should "update the document" do
121
+ #data = {"age" => "42", "lastname" => "arnold", "phone" => "202-456-1234", "hobbies" => "football,running, video gamess" }
122
+ data = {"age" => "13" }
123
+ doc = { :database => 'contacts', :doc_id => 'john', :data => data}
124
+ Document.update doc
125
+ end
126
+
120
127
  should "edit a document - handle exceptions" do
121
128
  begin
122
129
  data = {:firstname => 'john', :lastname =>'smith', :email => 'john@mail.com',:gender=>'male', :_rev=>'2-e813a0e902e3ac114400ff3959a2adde'}
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 4
9
- version: 0.2.4
8
+ - 5
9
+ version: 0.2.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Obi Akubue
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-07-20 00:00:00 -04:00
17
+ date: 2011-08-03 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -136,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
136
136
  requirements:
137
137
  - - ">="
138
138
  - !ruby/object:Gem::Version
139
- hash: 600156783
139
+ hash: 541704349
140
140
  segments:
141
141
  - 0
142
142
  version: "0"