public
Description: Library to access couchdb in ruby
Homepage: http://couchobject.rubyforge.org
Clone URL: git://github.com/halfbyte/couchobject.git
- Added Database#filter{|doc| .. } for doing temp queries with ruby
- db.store(document) [Thanks Malesca]
js (author)
Fri Sep 14 15:46:36 -0700 2007
commit  ae74f5c961866f86c7ac89278952835c54e14f9d
tree    196d516ee2d859d286550f1e20da3cd8fe949156
parent  293112fb247c3816c5afe5595b8047d998b6b93e
0
...
1
2
3
4
5
 
6
7
8
...
 
 
 
1
 
2
3
4
5
0
@@ -1,7 +1,4 @@
0
-- flat namespace for object attributes? (instead of the "attributes" key)
0
-- Query/View API
0
-- Database#filter for filtering documents with Ruby (with some ruby2js or RubyToRuby magic)
0
 - Expand on View class
0
-- db.store(doc) [as per Malesca's suggestion]
0
+- Do dot notation for document attributes in Database#filter
0
 
0
 - CouchObject::Model for more domain specific/clearer way to model Couch docs in ruby
0
\ No newline at end of file
...
9
10
11
 
12
 
13
14
15
16
 
17
18
19
...
9
10
11
12
13
14
15
16
17
18
19
20
21
22
0
@@ -9,11 +9,14 @@ rescue LoadError
0
   $stderr.puts "C version of json (fjson) could not be loaded, using pure ruby one"
0
   require "json/pure"
0
 end
0
+
0
 require 'json/add/core'
0
+require "ruby2ruby"
0
 
0
 $:.unshift File.dirname(__FILE__)
0
 
0
 require "couch_object/utils"
0
+require "couch_object/proc_condition"
0
 require "couch_object/document"
0
 require "couch_object/response"
0
 require "couch_object/server"
...
101
102
103
 
 
 
 
104
105
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
108
109
...
101
102
103
104
105
106
107
108
 
 
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
0
@@ -101,9 +101,25 @@ module CouchObject
0
       resp.to_document.rows
0
     end
0
     
0
+ def store(document)
0
+ document.save(self)
0
+ end
0
+
0
     # Queries the database with the block (using a temp. view)
0
- def filter(&blk)
0
-
0
+ # Requires a block argument that's the doc thats evaluted in
0
+ # CouchDb
0
+ #
0
+ # >> pp db.filter do |doc|
0
+ # if doc["foo"] == "baz"
0
+ # return doc["foo"]
0
+ # end
0
+ # end
0
+ # [{"_rev"=>928806717,
0
+ # "_id"=>"28D568C5992CBD2B4711F57225A19517",
0
+ # "value"=>"baz"}]
0
+ def filter(&block)
0
+ resp = Response.new(post("_temp_view", ProcCondition.new(&block).to_ruby)).parse
0
+ resp.to_document.rows
0
     end
0
     
0
     def views(view_name)
...
139
140
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
143
144
...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
0
@@ -139,5 +139,34 @@ describe CouchObject::Database do
0
     db.all_documents.should == [{"_rev"=>123, "_id"=>"123ABC"}]
0
   end
0
   
0
+ it "should store a document" do
0
+ db = CouchObject::Database.new(@uri, "foo")
0
+ doc = CouchObject::Document.new({"foo" => "bar"})
0
+ db.should_receive(:post).with("", JSON.unparse("foo" => "bar"))
0
+ db.store(doc)
0
+ end
0
+
0
+ it "should return the rows when filtering" do
0
+ db = CouchObject::Database.new(@uri, "foo")
0
+ rowdata = { "_rev"=>1,
0
+ "_id"=>"1",
0
+ "value"=> {
0
+ "_id"=>"1",
0
+ "_rev"=>1,
0
+ "foo"=>"bar"
0
+ }
0
+ }
0
+ resp = mock("response")
0
+ resp.stub!(:body).and_return(JSON.unparse("rows" => [rowdata]))
0
+ resp.stub!(:parse).and_return(resp)
0
+ resp.stub!(:to_document).and_return(
0
+ CouchObject::Document.new("rows" => [rowdata])
0
+ )
0
+ db.should_receive(:post).with("_temp_view", "proc { |doc|\n (doc[\"foo\"] == \"bar\")\n}").and_return(resp)
0
+ rows = db.filter{|doc| doc["foo"] == "bar"}
0
+ rows.size.should == 1
0
+ rows.first["value"]["foo"].should == "bar"
0
+ end
0
+
0
   #it "should url encode paths"
0
 end
0
\ No newline at end of file
...
75
76
77
 
 
 
 
 
 
 
 
 
 
 
 
 
78
...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
0
@@ -75,4 +75,17 @@ describe "Database operations" do
0
     resp.code.should == 404
0
   end
0
   
0
+ it "should filter documents" do
0
+ db = create_and_open_test_db
0
+ db.post("", JSON.unparse({"foo" => "bar"}))
0
+ db.post("", JSON.unparse({"foo" => "baz"}))
0
+ db.post("", JSON.unparse({"foo" => "qux"}))
0
+ db.all_documents.size.should == 3
0
+ results = db.filter do |doc|
0
+ doc["foo"] =~ /ba/
0
+ end
0
+ results.size.should == 2
0
+
0
+ end
0
+
0
 end

Comments

    No one has commented yet.