leanback 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +10 -0
  3. data/Gemfile +5 -6
  4. data/Gemfile.lock +65 -14
  5. data/README.md +465 -33
  6. data/Rakefile +8 -17
  7. data/VERSION +1 -1
  8. data/leanback.gemspec +26 -85
  9. data/lib/leanback.rb +129 -587
  10. data/spec/leanback_spec.rb +356 -0
  11. metadata +40 -117
  12. data/Changelog.rdoc +0 -146
  13. data/documentation/static/2011/11/20/i-moved-leanback-documentation/index.html +0 -180
  14. data/documentation/static/2011/11/20/index.html +0 -146
  15. data/documentation/static/2011/11/index.html +0 -146
  16. data/documentation/static/2011/index.html +0 -146
  17. data/documentation/static/2013/06/02/index.html +0 -146
  18. data/documentation/static/2013/06/02/released-leanback-v0-3-4/index.html +0 -180
  19. data/documentation/static/2013/06/09/index.html +0 -146
  20. data/documentation/static/2013/06/09/released-leanback-v0-4-0/index.html +0 -179
  21. data/documentation/static/2013/06/index.html +0 -158
  22. data/documentation/static/2013/index.html +0 -158
  23. data/documentation/static/author/admin/index.html +0 -175
  24. data/documentation/static/basic-couchdb-operations/index.html +0 -259
  25. data/documentation/static/category/uncategorized/index.html +0 -170
  26. data/documentation/static/couchdb-configuration/index.html +0 -189
  27. data/documentation/static/couchdb-security/index.html +0 -218
  28. data/documentation/static/count-by-multiple-documents/index.html +0 -221
  29. data/documentation/static/count-documents-by-key/index.html +0 -177
  30. data/documentation/static/css/2c-l-fixed.css +0 -1
  31. data/documentation/static/css/2c-l-fixed.dev.css +0 -62
  32. data/documentation/static/css/2c-r-fixed.css +0 -1
  33. data/documentation/static/css/2c-r-fixed.dev.css +0 -60
  34. data/documentation/static/css/3c-c-fixed.css +0 -1
  35. data/documentation/static/css/3c-c-fixed.dev.css +0 -84
  36. data/documentation/static/css/3c-l-fixed.css +0 -1
  37. data/documentation/static/css/3c-l-fixed.dev.css +0 -61
  38. data/documentation/static/css/3c-r-fixed.css +0 -1
  39. data/documentation/static/css/3c-r-fixed.dev.css +0 -62
  40. data/documentation/static/css/holy-grail-fluid.css +0 -5
  41. data/documentation/static/css/plugins.css +0 -5
  42. data/documentation/static/css/print.css +0 -5
  43. data/documentation/static/css/screen.css +0 -1
  44. data/documentation/static/design-documents-and-permanent-views/index.html +0 -454
  45. data/documentation/static/error-handling/index.html +0 -157
  46. data/documentation/static/find-document-by-multiple-keys/index.html +0 -269
  47. data/documentation/static/find-documents-by-key/index.html +0 -243
  48. data/documentation/static/index.html +0 -161
  49. data/documentation/static/leanback/index.html +0 -130
  50. data/documentation/static/leanback/installation/index.html +0 -119
  51. data/documentation/static/setting-the-bind_address-port/index.html +0 -151
  52. data/documentation/static/style.css +0 -16
  53. data/spec/admin_party/database_spec.rb +0 -400
  54. data/spec/no_admin_party/cloudant_spec.rb +0 -365
  55. data/spec/no_admin_party/database_spec.rb +0 -491
  56. data/spec/no_admin_party/non_admin_user_spec.rb +0 -67
  57. data/test/helper.rb +0 -18
  58. data/test/main.rb +0 -295
  59. data/test/my_view.json +0 -8
  60. data/test/my_views.json +0 -8
  61. data/test/start.json +0 -8
  62. data/test/test_leanback.rb +0 -319
  63. data/test/view_age.json +0 -8
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2d9de472fd26c48ab94e5499b1b94f8c3810dff9
4
+ data.tar.gz: ee96f42ee4f58fee1401d73caeb1555cb6bfa24f
5
+ SHA512:
6
+ metadata.gz: 3df3aeee3aed013e815de1e2fbd192ce8c9735c4c717727e5eb5b0590417cf470c03539998b8c3c5a178c0d96a928cd4e3864321eceb68e5c1a18b11cdeb377f
7
+ data.tar.gz: ac40f3567ef20d8bf369e527d285f7d87e21ba2c3ec53dfd656e2c2bbaf66292f5918d2ec0fa4d854d031e70ede30f2e7aacd3e49c8ef27b7088294f94734705
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 1.9.3
5
+ - jruby-19mode # JRuby in 1.9 mode
6
+
7
+ services:
8
+ - couchdb
9
+
10
+ script: bundle exec rspec -fd spec/leanback_spec.rb
data/Gemfile CHANGED
@@ -2,16 +2,15 @@ source "http://rubygems.org"
2
2
  source "http://gemcutter.org"
3
3
  # Add dependencies required to use your gem here.
4
4
  # Example:
5
- # gem "activesupport", ">= 2.3.5"
5
+ gem "activesupport"
6
6
  gem "rest-client"
7
- gem "yajl-ruby"
7
+ gem "json_pure"
8
8
 
9
9
  # Add dependencies to develop your gem here.
10
10
  # Include everything needed to run rake, tests, features, etc.
11
11
  group :development do
12
- gem "shoulda", ">= 0"
13
- gem "bundler", "~> 1.0.0"
14
- gem "jeweler", "~> 1.5.2"
15
- gem "rcov", ">= 0"
12
+ gem "pry"
13
+ gem "bundler"
14
+ gem "jeweler", "~> 1.8.7"
16
15
  gem "rspec"
17
16
  end
data/Gemfile.lock CHANGED
@@ -2,16 +2,65 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  remote: http://gemcutter.org/
4
4
  specs:
5
+ activesupport (4.0.2)
6
+ i18n (~> 0.6, >= 0.6.4)
7
+ minitest (~> 4.2)
8
+ multi_json (~> 1.3)
9
+ thread_safe (~> 0.1)
10
+ tzinfo (~> 0.3.37)
11
+ addressable (2.3.6)
12
+ atomic (1.1.16)
13
+ builder (3.2.2)
14
+ coderay (1.1.0)
5
15
  diff-lcs (1.1.2)
6
- git (1.2.5)
7
- jeweler (1.5.2)
8
- bundler (~> 1.0.0)
16
+ faraday (0.8.9)
17
+ multipart-post (~> 1.2.0)
18
+ git (1.2.6)
19
+ github_api (0.10.1)
20
+ addressable
21
+ faraday (~> 0.8.1)
22
+ hashie (>= 1.2)
23
+ multi_json (~> 1.4)
24
+ nokogiri (~> 1.5.2)
25
+ oauth2
26
+ hashie (2.1.1)
27
+ highline (1.6.21)
28
+ i18n (0.6.9)
29
+ jeweler (1.8.8)
30
+ builder
31
+ bundler (~> 1.0)
9
32
  git (>= 1.2.5)
33
+ github_api (= 0.10.1)
34
+ highline (>= 1.6.15)
35
+ nokogiri (= 1.5.10)
10
36
  rake
11
- mime-types (1.16)
12
- rake (0.8.7)
13
- rcov (0.9.9)
14
- rest-client (1.6.1)
37
+ rdoc
38
+ json (1.8.1)
39
+ json_pure (1.8.1)
40
+ jwt (0.1.11)
41
+ multi_json (>= 1.5)
42
+ method_source (0.8.2)
43
+ mime-types (1.25.1)
44
+ minitest (4.7.5)
45
+ multi_json (1.9.2)
46
+ multi_xml (0.5.5)
47
+ multipart-post (1.2.0)
48
+ nokogiri (1.5.10)
49
+ oauth2 (0.9.3)
50
+ faraday (>= 0.8, < 0.10)
51
+ jwt (~> 0.1.8)
52
+ multi_json (~> 1.3)
53
+ multi_xml (~> 0.5)
54
+ rack (~> 1.2)
55
+ pry (0.9.12.4)
56
+ coderay (~> 1.0)
57
+ method_source (~> 0.8)
58
+ slop (~> 3.4)
59
+ rack (1.5.2)
60
+ rake (10.3.1)
61
+ rdoc (4.1.1)
62
+ json (~> 1.4)
63
+ rest-client (1.6.7)
15
64
  mime-types (>= 1.16)
16
65
  rspec (2.6.0)
17
66
  rspec-core (~> 2.6.0)
@@ -21,17 +70,19 @@ GEM
21
70
  rspec-expectations (2.6.0)
22
71
  diff-lcs (~> 1.1.2)
23
72
  rspec-mocks (2.6.0)
24
- shoulda (2.11.3)
25
- yajl-ruby (0.8.2)
73
+ slop (3.4.7)
74
+ thread_safe (0.3.1)
75
+ atomic (>= 1.1.7, < 2)
76
+ tzinfo (0.3.38)
26
77
 
27
78
  PLATFORMS
28
79
  ruby
29
80
 
30
81
  DEPENDENCIES
31
- bundler (~> 1.0.0)
32
- jeweler (~> 1.5.2)
33
- rcov
82
+ activesupport
83
+ bundler
84
+ jeweler (~> 1.8.7)
85
+ json_pure
86
+ pry
34
87
  rest-client
35
88
  rspec
36
- shoulda
37
- yajl-ruby
data/README.md CHANGED
@@ -1,53 +1,485 @@
1
1
  ###Leanback
2
2
 
3
- Simple Ruby Interface to CouchDB.
3
+ [![Build Status](https://travis-ci.org/obi-a/leanback.svg?branch=master)](https://travis-ci.org/obi-a/leanback)
4
4
 
5
- This project is still under development. Not complete by any means. I made this Gem to use in my projects. I will expose more CouchDB features soon.
5
+ Simple Ruby Interface to CouchDB. This current version is a complete re-write with a new API.
6
6
 
7
- ##Goals
8
- * To create a simple Ruby Interface to CouchDB
9
- * Expose the features of CouchDB to the Ruby Lang.,
10
- * Use a minimalist Ruby DSL to access CouchDB
11
- * provide a very easy way to persist and retrieve data
7
+ ### Installation:
8
+ ```
9
+ gem install leanback
10
+ ```
12
11
 
13
- ##Compatibility
14
- Upgraded and tested on CouchDB 1.2.0. CouchDB 1.0.1 should still work fine.
12
+ ### Basic Operations:
13
+ Initialize leanback
15
14
 
16
- ##Changes
17
- View the [Changelog](https://github.com/obi-a/leanback/blob/master/Changelog.rdoc)
15
+ ```ruby
16
+ my_database = Leanback::Couchdb.new(database: "my_database")
17
+ #=> #<Leanback::Couchdb:0x000000034a7b78
18
+ # @address="http://127.0.0.1",
19
+ # @database="my_database",
20
+ # @password=nil,
21
+ # @port="5984",
22
+ # @username=nil>
23
+ ```
24
+ In the above code, my_database object will be used to perform operations on the couchDB database named "my_database". The above example assumes that the database is in admin party mode, since no username or password was provided.
18
25
 
19
- ##Usage
26
+ If there is no admin party then a username and password is required to perform the operations.
27
+ A username and password can be included during initialization:
28
+ ```ruby
29
+ my_database = Leanback::Couchdb.new(database: "my_database", username: "obi", password: "abc1234")
30
+ #=> #<Leanback::Couchdb:0x000000033fd970
31
+ # @address="http://127.0.0.1",
32
+ # @database="my_database",
33
+ # @password="abc1234",
34
+ # @port="5984",
35
+ # @username="obi">
20
36
 
37
+ ```
38
+ By default, Leanback uses couchDB's default http address and port (http://127.0.0.1:5984), to use a different address and port, include it in the initialization:
39
+ ```ruby
40
+ my_database = Leanback::Couchdb.new(database: "my_database", address: "https://obi.iriscouch.com", port: "6984")
41
+ #=> #<Leanback::Couchdb:0x000000033ab5d0
42
+ # @address="https://obi.iriscouch.com",
43
+ # @database="my_database",
44
+ # @password=nil,
45
+ # @port="6984",
46
+ # @username=nil>
47
+ ```
21
48
 
22
- * [Leanback](http://www.whisperservers.com/leanback/leanback/)
49
+ Create database:
50
+ ```ruby
51
+ my_database = Leanback::Couchdb.new(database: "my_database")
52
+ my_database.create
53
+ #=> {:ok=>true}
54
+ ```
23
55
 
24
- + [Installation](http://www.whisperservers.com/leanback/leanback/installation/)
25
-
26
- + [Basic CouchDB Operations](http://www.whisperservers.com/leanback/basic-couchdb-operations/)
27
-
28
- + [Find Documents by Key](http://www.whisperservers.com/leanback/find-documents-by-key/)
29
-
30
- + [Find Documents By Multiple Keys] (http://www.whisperservers.com/leanback/find-document-by-multiple-keys/)
56
+ Delete database
57
+ ```ruby
58
+ my_database.delete
59
+ #=> {:ok=>true}
60
+ ```
31
61
 
32
- + [Count Documents by Key](http://www.whisperservers.com/leanback/count-documents-by-key/)
62
+ Create a document
63
+ ```ruby
64
+ my_database.create_doc("linda", firstname: "linda", lastname: "smith")
65
+ #=> {:ok=>true, :id=>"linda", :rev=>"1-ff286690ab5b446a727840ce7420843a"}
66
+ ```
67
+ The created document inside couchDB will be:
68
+ ```javascript
69
+ {
70
+ "_id": "linda",
71
+ "_rev": "1-ff286690ab5b446a727840ce7420843a",
72
+ "firstname": "linda",
73
+ "lastname": "smith"
74
+ }
75
+ ```
33
76
 
34
- + [Count Documents by Multiple Keys] (http://www.whisperservers.com/leanback/count-by-multiple-documents/)
77
+ Delete the document with it's latest revision
78
+ ```ruby
79
+ my_database.delete_doc("linda", rev = "1-ff286690ab5b446a727840ce7420843a")
80
+ #=> {:ok=>true, :id=>"linda", :rev=>"2-d689d9b5b9f2ded6a2157fc9cc84a00f"}
81
+ ```
82
+ Delete the document without providing a revision
83
+ ```ruby
84
+ my_database.delete_doc!("linda")
85
+ #=> {:ok=>true, :id=>"linda", :rev=>"4-5d1a6851ec7562378caa4ce4adef9ee4"}
86
+ ```
87
+ delete_doc! with the bang, first fetches the document with the provided id, retrieves it's latest revision and then, deletes the document using the latest revision.
35
88
 
36
- + Working with [CouchDB Views](http://www.whisperservers.com/leanback/design-documents-and-permanent-views/)
89
+ Fetch the document using its id
90
+ ```ruby
91
+ my_database.get_doc('linda')
92
+ #=> {:_id=>"linda",
93
+ # :_rev=>"5-74894db03ef6d22e6a0e4ef90b5a85fb",
94
+ # :firstname=>"linda",
95
+ # :lastname=>"smith"}
37
96
 
38
- + [Error Handling](http://www.whisperservers.com/leanback/error-handling/)
39
-
40
- + [CouchDB Configuration](http://www.whisperservers.com/leanback/couchdb-configuration/)
41
-
42
- + [CouchDB Security](http://www.whisperservers.com/leanback/couchdb-security/)
43
-
44
- + [CouchDB Bind_Address & Port](http://www.whisperservers.com/leanback/setting-the-bind_address-port/)
97
+ ```
98
+
99
+ Update the document
100
+ ```ruby
101
+ my_database.update_doc("linda", firstname: "nancy", lastname: "drew", _rev: "5-74894db03ef6d22e6a0e4ef90b5a85fb")
102
+ #=> {:ok=>true, :id=>"linda", :rev=>"6-950d16c8c39daa77fad11de85b9467fc"}
103
+ ```
104
+ update_doc replaces the old document with the new data. A revision (_rev) must be provided in the data. The resulting document after the update will be
105
+ ```javascript
106
+ {
107
+ "_id": "linda",
108
+ "_rev": "6-950d16c8c39daa77fad11de85b9467fc",
109
+ "firstname": "nancy",
110
+ "lastname": "drew"
111
+ }
112
+
113
+ ```
114
+ Edit parts of a document
115
+ ```ruby
116
+ my_database.edit_doc!("linda", lastname: "brown", phone: "777-777-7777")
117
+ #=> {:ok=>true, :id=>"linda", :rev=>"7-e44206dd09d2740171576e5867fff7a1"}
118
+ ```
119
+ The edited version of the document is now:
120
+ ```javascript
121
+ {
122
+ "_id": "linda",
123
+ "_rev": "7-e44206dd09d2740171576e5867fff7a1",
124
+ "firstname": "nancy",
125
+ "lastname": "brown",
126
+ "phone": "777-777-7777"
127
+ }
128
+
129
+ ```
130
+
131
+ ###Working with Desgin Documents and views
132
+
133
+ Create a design document
134
+ ```ruby
135
+ design_doc = {
136
+ language: "javascript",
137
+ views: {
138
+ by_gender: {
139
+ map: "function(doc){ if(doc.gender) emit(doc.gender); }"
140
+ }
141
+ }
142
+ }
143
+ my_database.create_doc "_design/my_doc", design_doc
144
+ #=> {:ok=>true, :id=>"_design/my_doc", :rev=>"1-4939535c4d51fb5bcbc9b32e2f58e755"}
145
+ ```
146
+
147
+ Query a permanent view
148
+ ```ruby
149
+ my_database.view("_design/my_doc", "by_gender")
150
+ #=> {:total_rows=>7,
151
+ # :offset=>0,
152
+ # :rows=>
153
+ # [{:id=>"christina", :key=>"female", :value=>nil},
154
+ # {:id=>"lisa", :key=>"female", :value=>nil},
155
+ # {:id=>"nancy", :key=>"female", :value=>nil},
156
+ # {:id=>"susan", :key=>"female", :value=>nil},
157
+ # {:id=>"james", :key=>"male", :value=>nil},
158
+ # {:id=>"kevin", :key=>"male", :value=>nil},
159
+ # {:id=>"martin", :key=>"male", :value=>nil}]}
160
+ ```
161
+ The view() method is used to query parmanent views. It takes the design document name and view name as arguments. It can also optionally take the following CouchDB query options in a hash as arguments: key, limit, skip, descending, include_docs, reduce, startkey, starkey_docid, endkey, endkey_docid, inclusive_end, stale, group, group_level.
162
+
163
+ To query a permanent view by key
164
+ ```ruby
165
+ my_database.view("_design/my_doc", "by_gender", key: '"male"')
166
+ #=> {:total_rows=>7,
167
+ # :offset=>4,
168
+ # :rows=>
169
+ # [{:id=>"james", :key=>"male", :value=>nil},
170
+ # {:id=>"kevin", :key=>"male", :value=>nil},
171
+ # {:id=>"martin", :key=>"male", :value=>nil}]}
172
+ ```
173
+ The above example sends a query to the view using the key "male" and returns all documents with "gender" equal to "male".
174
+
175
+ To include actual documents in the query results, we can add include_docs to the query options
176
+ ```ruby
177
+ my_database.view("_design/my_doc", "by_gender", key: '"male"', include_docs: true)
178
+ #=> {:total_rows=>7,
179
+ # :offset=>4,
180
+ # :rows=>
181
+ # [{:id=>"james",
182
+ # :key=>"male",
183
+ # :value=>nil,
184
+ # :doc=>
185
+ # {:_id=>"james",
186
+ # :_rev=>"1-56ff4f73369bdf8350615a58e12e4c3b",
187
+ # :firstname=>"james",
188
+ # :state=>"new york",
189
+ # :gender=>"male",
190
+ # :city=>"manhattan",
191
+ # :age=>23}},
192
+ # {:id=>"kevin",
193
+ # :key=>"male",
194
+ # :value=>nil,
195
+ # :doc=>
196
+ # {:_id=>"kevin",
197
+ # :_rev=>"1-3c6381603d9f15cb966948eb29218cf7",
198
+ # :firstname=>"kevin",
199
+ # :state=>"new york",
200
+ # :gender=>"male",
201
+ # :city=>"bronx",
202
+ # :age=>37}},
203
+ # {:id=>"martin",
204
+ # :key=>"male",
205
+ # :value=>nil,
206
+ # :doc=>
207
+ # {:_id=>"martin",
208
+ # :_rev=>"1-41956cd527d75643171919731abd97c0",
209
+ # :firstname=>"martin",
210
+ # :state=>"new york",
211
+ # :gender=>"male",
212
+ # :city=>"manhattan",
213
+ # :age=>29}}]}
214
+ ```
215
+
216
+ To return results in descending order:
217
+ ```ruby
218
+ my_database.view("_design/my_doc", "by_gender", key: '"male"', descending: true)
219
+ #=> {:total_rows=>7,
220
+ # :offset=>0,
221
+ # :rows=>
222
+ # [{:id=>"martin", :key=>"male", :value=>nil},
223
+ # {:id=>"kevin", :key=>"male", :value=>nil},
224
+ # {:id=>"james", :key=>"male", :value=>nil}]}
225
+ ```
226
+
227
+ To limit the number of documents returned from the query
228
+ ```ruby
229
+ my_database.view("_design/my_doc", "by_gender", limit: 4)
230
+ #=> {:total_rows=>7,
231
+ # :offset=>0,
232
+ # :rows=>
233
+ # [{:id=>"christina", :key=>"female", :value=>nil},
234
+ # {:id=>"lisa", :key=>"female", :value=>nil},
235
+ # {:id=>"nancy", :key=>"female", :value=>nil},
236
+ # {:id=>"susan", :key=>"female", :value=>nil}]}
237
+ ```
238
+
239
+ Skip some documents in the query
240
+ ```ruby
241
+ my_database.view("_design/my_doc", "by_gender", skip: 2)
242
+ #=> {:total_rows=>7,
243
+ # :offset=>2,
244
+ # :rows=>
245
+ # [{:id=>"nancy", :key=>"female", :value=>nil},
246
+ # {:id=>"susan", :key=>"female", :value=>nil},
247
+ # {:id=>"james", :key=>"male", :value=>nil},
248
+ # {:id=>"kevin", :key=>"male", :value=>nil},
249
+ # {:id=>"martin", :key=>"male", :value=>nil}]}
250
+ ```
251
+
252
+ Query views by startkey and endkey
253
+ ```ruby
254
+ design_doc = {
255
+ language: "javascript",
256
+ views: {
257
+ people_by_age: {
258
+ map: "function(doc){ if(doc.age) emit(doc.age); }"
259
+ }
260
+ }
261
+ }
262
+ my_database.create_doc "_design/ages", design_doc
263
+
264
+ my_database.view("_design/ages", "people_by_age", startkey: 20, endkey: 29)
265
+ #=> {:total_rows=>7,
266
+ # :offset=>0,
267
+ # :rows=>
268
+ # [{:id=>"christina", :key=>22, :value=>nil},
269
+ # {:id=>"james", :key=>23, :value=>nil},
270
+ # {:id=>"nancy", :key=>25, :value=>nil},
271
+ # {:id=>"martin", :key=>29, :value=>nil}]}
272
+ ```
273
+ The above returns documents with age between 20 and 29.
274
+
275
+ Another example to return documents with age 31 and over
276
+ ```ruby
277
+ my_database.view("_design/ages", "people_by_age", startkey: 31)
278
+ #=> {:total_rows=>7,
279
+ # :offset=>4,
280
+ # :rows=>
281
+ # [{:id=>"lisa", :key=>31, :value=>nil},
282
+ # {:id=>"susan", :key=>35, :value=>nil},
283
+ # {:id=>"kevin", :key=>37, :value=>nil}]}
284
+ ```
285
+
286
+ Working with compound startkey and endkey
287
+ ```ruby
288
+ my_database.view("_design/gender_city", "people_by_gender_and_city", startkey: ["female", "bronx", 25].to_s, endkey: ["female", "bronx", 25].to_s)
289
+ #=> {:total_rows=>6,
290
+ # :offset=>1,
291
+ # :rows=>[{:id=>"nancy", :key=>["female", "bronx", 25], :value=>nil}]}
292
+ ```
293
+ ###Dynamic Queries
294
+ Dynamic queries can be performed on documents using the where() helper method, example to fetch all documents that match the key/value pairs {city: "bronx", gender: "female"}
295
+ ```ruby
296
+ my_database.where(city: "bronx", gender: "female")
297
+ #=> [{:_id=>"christina",
298
+ # :_rev=>"1-e9782aa92f7d88eb5dc5e1a878c8e193",
299
+ # :firstname=>"christina",
300
+ # :state=>"new york",
301
+ # :gender=>"female",
302
+ # :city=>"bronx",
303
+ # :age=>22},
304
+ # {:_id=>"nancy",
305
+ # :_rev=>"1-44ac471d9e6433eaa6e67607c7a175c9",
306
+ # :firstname=>"nancy",
307
+ # :state=>"new york",
308
+ # :gender=>"female",
309
+ # :city=>"bronx",
310
+ # :age=>25}]
311
+ ```
312
+ To fetch all documents that match the key/value pairs {state: "new york", fullname: ["susan", "Lee"]}
313
+ ```ruby
314
+ my_database.where(state: "new york", fullname: ["susan", "Lee"])
315
+ #=> [{:_id=>"susan",
316
+ # :_rev=>"1-11b05eacc247b8541fa6c659f26447de",
317
+ # :firstname=>"susan",
318
+ # :state=>"new york",
319
+ # :gender=>"female",
320
+ # :age=>35,
321
+ # :fullname=>["susan", "Lee"]}]
322
+ ```
323
+ Similar to view(), the where() method also supports options skip, limit, descending. Example to return documents in descending order;
324
+ ```ruby
325
+ my_database.where({city: "bronx", gender: "female"}, descending: true)
326
+ #=> [{:_id=>"nancy",
327
+ # :_rev=>"1-44ac471d9e6433eaa6e67607c7a175c9",
328
+ # :firstname=>"nancy",
329
+ # :state=>"new york",
330
+ # :gender=>"female",
331
+ # :city=>"bronx",
332
+ # :age=>25},
333
+ # {:_id=>"christina",
334
+ # :_rev=>"1-e9782aa92f7d88eb5dc5e1a878c8e193",
335
+ # :firstname=>"christina",
336
+ # :state=>"new york",
337
+ # :gender=>"female",
338
+ # :city=>"bronx",
339
+ # :age=>22}]
340
+ ```
341
+ Limit to one result
342
+ ```ruby
343
+ my_database.where({city: "bronx", gender: "female"}, limit: 1)
344
+ #=> [{:_id=>"christina",
345
+ # :_rev=>"1-e9782aa92f7d88eb5dc5e1a878c8e193",
346
+ # :firstname=>"christina",
347
+ # :state=>"new york",
348
+ # :gender=>"female",
349
+ # :city=>"bronx",
350
+ # :age=>22}]
351
+ ```
352
+ Calling the where() method, for the first time creates a view in the database for provided keys. Subsequent calls to where() will simply query the previously created view and return the documents. For example calling the method below:
353
+ ```ruby
354
+ my_database.where(city: "bronx", gender: "female")
355
+ ```
356
+ Will add the following document to the database,
357
+ ```javascript
358
+ {
359
+ "_id": "_design/city_gender_keys_finder",
360
+ "_rev": "1-41fb3b17c8b99be176928e3ea5588935",
361
+ "language": "javascript",
362
+ "views": {
363
+ "find_by_keys_city_gender": {
364
+ "map": "function(doc){ if(doc.city && doc.gender) emit([doc.city,doc.gender]);}"
365
+ }
366
+ }
367
+ }
368
+ ```
369
+ And then query it with:
370
+ ```
371
+ /_design/city_gender_keys_finder/_view/find_by_keys_city_gender?endkey=["bronx", "female"]&include_docs=true&startkey=["bronx", "female"]
372
+ ```
373
+ Subsequent method calls will simply query the view and return the documents. where() is just a convienient helper method.
374
+
375
+ ###Security Object:
376
+ To set the security object for the database:
377
+ ```ruby
378
+ security_settings = { admins: {names: ["david"], roles: ["admin"]},
379
+ readers: {names: ["david"],roles: ["admin"]}
380
+ }
381
+
382
+ my_database.security_object = security_settings
383
+ #=> {:admins=>{:names=>["david"], :roles=>["admin"]},
384
+ # :readers=>{:names=>["david"], :roles=>["admin"]}}
385
+ ```
386
+
387
+ To retrieve the security object at anytime:
388
+ ```ruby
389
+ my_database.security_object
390
+ #=> {:admins=>{:names=>["david"], :roles=>["admin"]},
391
+ # :readers=>{:names=>["david"], :roles=>["admin"]}}
392
+ ```
393
+
394
+ ###CouchDB Configuration
395
+ CouchDB's configuration settings can be set using the set_config() method:
396
+ ```ruby
397
+ config = Leanback::Couchdb.new
398
+ config.set_config("section", "option", '"value"')
399
+ ```
400
+ For example to set couchDB's couch_httpd_auth timeout value:
401
+ ```ruby
402
+ config.set_config("couch_httpd_auth", "timeout", '"1600"')
403
+ #=> true
404
+ ```
405
+ This sets the CouchDB auth timeout to 1600 seconds.
406
+
407
+ To retrieve the configuration:
408
+ ```ruby
409
+ config.get_config("couch_httpd_auth", "timeout")
410
+ #=> "\"1600\"\n"
411
+ ```
412
+ To delete a configuration:
413
+ ```ruby
414
+ config.delete_config("section", "option")
415
+ #=> true
416
+ ```
417
+ A more useful example to add an admin user to couchDB with username and password;
418
+ ```ruby
419
+ config.set_config("admins", username = "james", password = '"abc123"')
420
+ #=> true
421
+ ```
422
+ This will add a CouchDB admin with username james, and password abc123. If couchDB was in admin party mode, this would end the party.
423
+
424
+ ##API Specification
425
+
426
+ ```ruby
427
+ #JSON result keys are automatically symoblized:
428
+ #returns data directly as couchdb returns them unaltered as ruby hash
429
+ c = Leanback::Couchdb.new database: xxxxx, username: xxxxx, password: xxxx, address: xxxxx, port: xxxxx
430
+
431
+ c.create
432
+ c.delete
433
+ c.create_doc id, {}
434
+ c.delete_doc id, rev
435
+ c.delete_doc! id
436
+ c.update_doc id, {} #hash includes rev
437
+ c.edit_doc! id, {}
438
+ c.get_doc id
439
+
440
+ options = { limit: x, skip: x, descending: x}
441
+ c.where {}, options
442
+
443
+ #create a design doc
444
+ design_doc = {
445
+ language: "javascript",
446
+ views: {
447
+ get_emails: {
448
+ map: "function(doc){ if(doc.firstname && doc.email) emit(doc.id,{Name: doc.firstname, Email: doc.email}); }"
449
+ }
450
+ }
451
+ }
452
+ c.create_doc "_design/my_doc", design_doc
453
+
454
+ #query a view
455
+ options = { limit: x, key: x, start_key: x, end_key: x, skip: x, descending: x, include_docs: boolean}
456
+ design_doc_name = "_design/my_doc"
457
+ view_name = "get_emails"
458
+ c.view design_doc_name, view_name, options
459
+
460
+ security_settings = { admins: {names: ["david"], roles: ["admin"]},
461
+ readers: {names: ["david"],roles: ["admin"]}
462
+ }
463
+
464
+ c.security_object = security_settings
465
+
466
+ c.security_object
467
+ #=> {:admins=>{:names=>["david"], :roles=>["admin"]},
468
+ # :readers=>{:names=>["david"], :roles=>["admin"]}}
469
+
470
+ c = Leanback::Couchdb.new
471
+ c.set_config("couch_httpd_auth", "timeout", '"900"')
472
+ c.get_config("couch_httpd_auth", "timeout")
473
+ #=> "\"900\"\n"
474
+ c.delete_config("section", "option")
475
+ ```
476
+
477
+ ##Supported Rubies
478
+ jruby-19mode, MRI 1.9.3 - 2.x
45
479
 
46
480
  ##License
47
481
  MIT License.
48
482
 
49
483
  ##Copyright
50
484
 
51
- Copyright (c) 2013 Obi Akubue.
52
-
53
-
485
+ Copyright (c) 2014 Obi Akubue.