3 CouchObject is a set of classes to help you talk to CouchDb (http://couchdbwiki.com/) with Ruby.
5 * Author: Johan Sørensen
6 * Contact: johan (at) johansorensen DOT com
7 * Home: http://rubyforge.org/projects/couchobject/
8 * Source (Git): http://repo.or.cz/w/couchobject.git
10 == Creating, opening and deleting databases
12 CouchObject::Database is the first interaction point to your CouchDb. Creating a CouchDb database:
14 >> CouchObject::Database.create!("http://localhost:8888", "mydb")
16 >> CouchObject::Database.all_databases("http://localhost:8888")
17 => ["couchobject", "couchobject_test", "foo", "mydb"]
18 >> db = CouchObject::Database.open("http://localhost:8888/mydb")
19 => #<CouchObject::Database:0x642fa8 @server=#<CouchObject::Server:0x642ef4 @connection=#<Net::HTTP localhost:8888 open=false>, @port=8888, @uri=#<URI::HTTP:0x321612 URL:http://localhost:8888>, @host="localhost">, @uri="http://localhost:8888", @dbname="mydb">
22 >> CouchObject::Database.delete!("http://localhost:8888", "mydb")
24 >> CouchObject::Database.all_databases("http://localhost:8888").include?("mydb")
27 === Interacting with the database
29 >> db.get("_all_docs")
30 => #<CouchObject::Response:0x14ed364 @response=#<Net::HTTPOK 200 OK readbody=true>, @parsed_body={"rows"=>[], "view"=>"_all_docs"}>
32 Issueing CouchObject::Database#get, CouchObject::Database#post, CouchObject::Database#put and CouchObject::Database#delete requests will return a CouchObject::Response object
34 >> db.get("_all_docs").body
35 => "{\"view\":\"_all_docs\", \"rows\":[\n\n]}"
36 >> db.get("_all_docs").parsed_body
37 => {"rows"=>[], "view"=>"_all_docs"}
38 >> db.post("", JSON.unparse({"foo" => "bar"}))
39 => #<CouchObject::Response:0x14d7780 @response=#<Net::HTTPCreated 201 Created readbody=true>, @parsed_body={"_rev"=>-992681820, "_id"=>"1206189E4496409DAD3818D241F5478F", "ok"=>true}>
40 >> db.get("_all_docs").parsed_body
41 => {"rows"=>[{"_rev"=>-992681820, "_id"=>"1206189E4496409DAD3818D241F5478F"}], "view"=>"_all_docs"}
42 >> db.get("1206189E4496409DAD3818D241F5478F").parsed_body
43 => {"_rev"=>-992681820, "_id"=>"1206189E4496409DAD3818D241F5478F", "foo"=>"bar"}
44 >> db.delete("1206189E4496409DAD3818D241F5478F").parsed_body
45 => {"_rev"=>548318611, "ok"=>true}
46 >> db.get("_all_docs").parsed_body
47 => {"rows"=>[], "view"=>"_all_docs"}
49 == The Couch View Requestor
51 couch_ruby_view_requestor is a JsServer client for CouchDb, allowing you to query documents with Ruby instead of Javascript! All you need to do is pass in a string with something that reponds to #call with one argument (the document):
53 >> db.post("_temp_view", "proc { |doc| doc[\"foo\"] =~ /ba/ }").parsed_body["rows"]
54 => [{"_rev"=>928806717, "_id"=>"28D568C5992CBD2B4711F57225A19517", "value"=>0}, {"_rev"=>-1696868121, "_id"=>"601D858DB2E298EFC4BBA92A11760D1E", "value"=>0}, {"_rev"=>-2093091288, "_id"=>"CABCEB3F2C8B70B3FE24A03FF6AB7A1E", "value"=>0}]
55 >> pp db.post("_temp_view", "proc { |doc| doc[\"foo\"] =~ /ba/ }").parsed_body["rows"]
56 [{"_rev"=>928806717, "_id"=>"28D568C5992CBD2B4711F57225A19517", "value"=>0},
57 {"_rev"=>-1696868121, "_id"=>"601D858DB2E298EFC4BBA92A11760D1E", "value"=>0},
58 {"_rev"=>-2093091288, "_id"=>"CABCEB3F2C8B70B3FE24A03FF6AB7A1E", "value"=>0}]
60 But you can even do it in plain Ruby, as opposed to a string, with Database#filter:
63 ?> if doc["foo"] == "bar"
67 => [{"_rev"=>-1696868121, "_id"=>"601D858DB2E298EFC4BBA92A11760D1E", "value"=>{"_id"=>"601D858DB2E298EFC4BBA92A11760D1E", "_rev"=>-1696868121, "foo"=>"bar"}}, {"_rev"=>-2093091288, "_id"=>"CABCEB3F2C8B70B3FE24A03FF6AB7A1E", "value"=>{"_id"=>"CABCEB3F2C8B70B3FE24A03FF6AB7A1E", "_rev"=>-2093091288, "foo"=>"bar"}}]
69 It requires that you set JsServer in your couch.ini to the path of the couch_ruby_view_requestor executable
71 == The Document object
73 CouchObject::Document wraps a few things in a nice api. In particular you can use it if you don't want to deal with hashes all the time (similar to ActiveRecord and so on):
75 >> doc = CouchObject::Document.new({ "foo" => [1,2], "bar" => true })
76 => #<CouchObject::Document:0x14a7224 @id=nil, @attributes={"foo"=>[1, 2], "bar"=>true}, @revision=nil>
86 You can also save a document to the database:
91 => #<CouchObject::Response:0x149f358 @response=#<Net::HTTPCreated 201 Created readbody=true>, @parsed_body={"_rev"=>2030456697, "_id"=>"CAEADDC895AC4D506542A3796CCA355D", "ok"=>true}>
93 => "CAEADDC895AC4D506542A3796CCA355D"
95 Since CouchObject::Database#get returns a CouchObject::Response object we can convert it into a Document instance easily with CouchObject::Database#to_document:
97 >> response = db.get(doc.id)
98 => #<CouchObject::Response:0x1498b98 @response=#<Net::HTTPOK 200 OK readbody=true>, @parsed_body={"_rev"=>2030456697, "_id"=>"CAEADDC895AC4D506542A3796CCA355D", "foo"=>[1, 2], "bar"=>true}>
99 >> the_doc_we_just_saved = response.to_document
100 => #<CouchObject::Document:0x148415c @id="CAEADDC895AC4D506542A3796CCA355D", @attributes={"foo"=>[1, 2], "bar"=>true}, @revision=2030456697>
101 >> the_doc_we_just_saved.foo
106 => #<CouchObject::Response:0x4b0adc @response=#<Net::HTTPCreated 201 Created readbody=true>, @parsed_body={"_rev"=>1670064786, "_id"=>"B4077269D2DF8433D145DC0702B9791C", "ok"=>true}>
109 == CouchObject::Persistable
111 It all started with this module, it maps ruby objects to CouchDb documents, using two mapping methods. It's highly experimental and may go away n future releases
114 require "couch_object"
116 include CouchObject::Persistable
118 def initialize(wheels)
121 attr_accessor :wheels
127 def self.from_couch(attributes)
128 new(attributes["wheels"])
132 By including the CouchObject::Persistable and defining two methods on our class we specify how we should serialize and deserialize our object to and from a CouchDb:
134 >> bike_4wd = Bike.new(4)
135 => #<Bike:0x6a0a68 @wheels=4>
136 >> bike_4wd.save("http://localhost:8888/couchobject")
137 => {"_rev"=>1745167971, "_id"=>"6FA2AFB623A93E0E77DEAAF59BB02565", "ok"=>true}
138 >> bike = Bike.get_by_id("http://localhost:8888/couchobject", bike_4wd.id)
139 => #<Bike:0x64846c @wheels=4>