leanback 0.1.8 → 0.2.0

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/README.rdoc CHANGED
@@ -77,6 +77,63 @@ Retrieve all documents in a database
77
77
  puts d["phone"]
78
78
  end
79
79
 
80
+ ===To Find documents by key
81
+ First add a finder/index to the database. Let's say we want to add an index to quickly search the database by an email value.
82
+ Couchdb.add_finder(:database => 'contacts', :key => 'email')
83
+
84
+ We have just added a find_by_email index to the database.
85
+
86
+ Now to find documents by email key (example: to find all contacts with email = 'nancy@mail.com')
87
+ Couchdb.find_by( :database => 'contacts', :email => 'nancy@mail.com')
88
+ # => [{"_id"=>"Nancy", "_rev"=>"1-d15a83d2a23b495c19df2595b636ecc8", "firstname"=>"Nancy", "lastname"=>"Lee",
89
+ "phone"=>"347-808-3734", "email"=>"nancy@mail.com", "gender"=>"female"}]
90
+
91
+ ====How the Find by key works
92
+ The method
93
+ Couchdb.add_finder(:database => 'contacts', :key => 'email')
94
+ creates a new design document in the contacts database called _design/email_finder with a view called find_by_email
95
+ {
96
+ "_id": "_design/email_finder",
97
+ "_rev": "7-da7f3c0bf183f4a36a82013bd0ea6537",
98
+ "language": "javascript",
99
+ "views": {
100
+ "find_by_email": {
101
+ "map": "function(doc){
102
+ if(doc.email)
103
+ emit(doc.email,doc);
104
+ }"
105
+ }
106
+ }
107
+ }
108
+
109
+ The method
110
+ Couchdb.find_by( :database => 'contacts', :email => 'nancy@mail.com')
111
+ will query the find_by_email view created above, with a
112
+ GET http://127.0.0.1:5984/contacts/_design/email_finder/_view/find_by_email?key="nancy@mail.com"
113
+ This returns all contacts with email = 'nancy@mail.com'.
114
+ Leanback parses the native JSON results to return only the data values.
115
+
116
+ ====Usage
117
+ You also can do
118
+ docs = Couchdb.find_by( :database => 'contacts', :email => 'nancy@mail.com')
119
+ docs.each do |d|
120
+ puts d["_rev"]
121
+ puts d["_id"]
122
+ puts d["firstname"]
123
+ puts d["lastname"]
124
+ puts d["email"]
125
+ puts d["phone"]
126
+ end
127
+
128
+ # => rev1-d15a83d2a23b495c19df2595b636ecc8
129
+ Nancy
130
+ Nancy
131
+ Lee
132
+ nancy@mail.com
133
+ 347-808-3734
134
+
135
+
136
+
80
137
  ===Query a permanent view
81
138
  view = { :database => "contacts", :design_doc => 'my_views', :view => 'get_female_contacts'}
82
139
  Couchdb.find view
@@ -108,7 +165,7 @@ First define the views in a JSON file
108
165
 
109
166
  Now create the design document and add the json file
110
167
  doc = { :database => 'contacts', :design_doc => 'more_views', :json_doc => '/path/to/my_views.json' }
111
- hash = Couchdb.create_design doc
168
+ Couchdb.create_design doc
112
169
  # => {"ok"=>true, "id"=>"_design/more_views", "rev"=>"1-d67ae97ff03a98f68ddc300bf9ae5048"}
113
170
 
114
171
  To query the view
@@ -137,20 +194,17 @@ You can always check this value
137
194
  Leanback uses the default Couchdb bind address http://127.0.0.1:5984. To use a different bind address;
138
195
  Couchdb.address = '192.168.2.16'
139
196
  Couchdb.port = '6000'
140
- Couchdb.all
141
197
  ...
142
198
  Document.address = '192.168.2.16'
143
199
  Document.port = '6000'
144
- docs = Couchdb.docs_from 'contacts'
200
+
145
201
 
146
202
  To change it back to default bind address at anytime, simply set the values to nil
147
203
  Couchdb.address = nil
148
204
  Couchdb.port = nil
149
- Couchdb.all
150
205
  ...
151
206
  Document.address = nil
152
207
  Document.port = nil
153
- docs = Couchdb.docs_from 'contacts'
154
208
 
155
209
 
156
210
  == Copyright
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.8
1
+ 0.2.0
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.1.8"
8
+ s.version = "0.2.0"
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-04}
12
+ s.date = %q{2011-07-11}
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
@@ -71,7 +71,6 @@ module Couchdb
71
71
  hash = Yajl::Parser.parse(e.response.to_s)
72
72
  end
73
73
  end
74
-
75
74
 
76
75
  #delete a database
77
76
  def self.delete(database_name)
@@ -109,13 +108,17 @@ def self.view(doc)
109
108
  end
110
109
 
111
110
  #query a permanent view
112
- def self.find(doc)
111
+ def self.find(doc,key=nil)
113
112
  set_address
114
113
  db_name = doc[:database]
115
114
  design_doc_name = doc[:design_doc]
116
115
  view_name = doc[:view]
117
116
  begin
117
+ if key == nil
118
118
  response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name + '/_view/' + view_name
119
+ else
120
+ response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name + '/_view/' + view_name + URI.escape('?key="' + key + '"')
121
+ end
119
122
  hash = Yajl::Parser.parse(response.to_str)
120
123
  rows = hash["rows"]
121
124
  count = 0
@@ -125,7 +128,8 @@ def self.find(doc)
125
128
  end
126
129
  return rows
127
130
  rescue => e
128
- hash = Yajl::Parser.parse(e.response.to_s)
131
+ puts e.inspect
132
+ #hash = Yajl::Parser.parse(e.response.to_s)
129
133
  end
130
134
  end
131
135
 
@@ -153,6 +157,46 @@ def self.create_design(doc)
153
157
  end
154
158
  end
155
159
 
160
+ #add a finder method to the database
161
+ #this creates a find by key method
162
+ def self.add_finder(options)
163
+ set_address
164
+ db_name = options[:database]
165
+ key = options[:key]
166
+ design_doc_name = key + '_finder'
167
+
168
+ view ='{
169
+ "language" : "javascript",
170
+ "views" :{
171
+ "find_by_'+key+'" : {
172
+ "map" : "function(doc){
173
+ if(doc.'+key+')
174
+ emit(doc.'+key+',doc);
175
+ }"
176
+ }
177
+ }
178
+ }'
179
+
180
+ begin
181
+ response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, view, {:content_type => :json, :accept => :json}
182
+ rescue => e
183
+ hash = Yajl::Parser.parse(e.response.to_s)
184
+ end
185
+ end
186
+
187
+ #find by key
188
+ def self.find_by(options)
189
+ set_address
190
+ db_name = options[:database]
191
+ index = options.keys[1].to_s
192
+ search_term = options.values[1]
193
+ design_doc_name = index + '_finder'
194
+ view_name = 'find_by_' + index
195
+
196
+ view = { :database => db_name, :design_doc => design_doc_name, :view => view_name}
197
+ find view,search_term
198
+ end
199
+
156
200
  #return a list of all docs in the database
157
201
  def self.docs_from(database_name)
158
202
  set_address
@@ -18,6 +18,15 @@ class TestLeanback < Test::Unit::TestCase
18
18
  #puts hash.inspect
19
19
  end
20
20
 
21
+ should "add a finder method to the database" do
22
+ hash = Couchdb.add_finder(:database => 'contacts', :key => 'email')
23
+ #puts hash.inspect
24
+ end
25
+
26
+ should "find items by key" do
27
+ docs = Couchdb.find_by( :database => 'contacts', :email => 'nancy@mail.com')
28
+ puts docs.inspect
29
+ end
21
30
 
22
31
  should "view document doc" do
23
32
 
@@ -30,7 +39,7 @@ class TestLeanback < Test::Unit::TestCase
30
39
  view = { :database => "contacts", :design_doc => 'more_views', :view => 'get_email'}
31
40
  puts 'viewing design doc...'
32
41
  hash = Couchdb.find view
33
- puts hash.inspect
42
+ #puts hash.inspect
34
43
  end
35
44
 
36
45
  should "Create a design doc/permanent view" do
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 8
9
- version: 0.1.8
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
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-04 00:00:00 -04:00
17
+ date: 2011-07-11 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -135,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
- hash: 116332943
138
+ hash: 929290637
139
139
  segments:
140
140
  - 0
141
141
  version: "0"