Class: CouchObject::Database
- Inherits:
-
Object
- Object
- CouchObject::Database
- Defined in:
- lib/couch_object/database.rb
Overview
A CouchDb database object
Instance Attribute Summary collapse
-
#server ⇒ Object
Returns the value of attribute server.
Class Method Summary collapse
-
.all_databases(uri) ⇒ Object
All databases at
uri
. -
.create(uri, dbname) ⇒ Object
Create a new database at
uri
with the name ifdbname
. -
.delete!(uri, dbname) ⇒ Object
Delete the database at
uri
with the name ifdbname
. -
.open(uri) ⇒ Object
Open a connection to the database at
uri
, whereuri
is a full uri like: localhost:5984/foo.
Instance Method Summary collapse
-
#[](id) ⇒ Object
Get a document by id.
-
#all_documents ⇒ Object
Returns an Array of all the documents in this db.
-
#delete(path, revision) ⇒ Object
Send a DELETE request to the
path
which is relative to the database path so calling with with “bar” as the path in the “foo_db” database will call host:port/foo_db/bar. -
#document(id, revision = nil) ⇒ Object
Get a document by
id
, optionally a specificrevision
too. -
#get(path) ⇒ Object
Send a GET request to the
path
which is relative to the database path so calling with with “bar” as the path in the “foo_db” database will call host:port/foo_db/bar. -
#initialize(uri, dbname) ⇒ Database
constructor
A new instance of Database.
-
#name ⇒ Object
Name of this database.
-
#post(path, payload, content_type = "application/json") ⇒ Object
Send a POST request to the
path
which is relative to the database path so calling with with “bar” as the path in the “foo_db” database will call host:port/foo_db/bar. -
#put(path, payload = "") ⇒ Object
Send a PUT request to the
path
which is relative to the database path so calling with with “bar” as the path in the “foo_db” database will call host:port/foo_db/bar. - #store(document) ⇒ Object
-
#url ⇒ Object
The full url of this database, eg localhost:5984/foo.
- #views(view_name) ⇒ Object
Constructor Details
Instance Attribute Details
#server ⇒ Object
Returns the value of attribute server.
40 41 42 |
# File 'lib/couch_object/database.rb', line 40 def server @server end |
Class Method Details
.all_databases(uri) ⇒ Object
All databases at uri
19 20 21 22 23 24 25 |
# File 'lib/couch_object/database.rb', line 19 def self.all_databases(uri) # FIXME: Move to Server ? server = Server.new(uri) resp = server.get("/_all_dbs") response = Response.new(resp).parse response.parsed_body end |
.create(uri, dbname) ⇒ Object
Create a new database at uri
with the name if dbname
5 6 7 8 9 |
# File 'lib/couch_object/database.rb', line 5 def self.create(uri, dbname) server = Server.new(uri) response = Response.new(server.put("/#{dbname}", "")).parse response.parsed_body end |
.delete!(uri, dbname) ⇒ Object
Delete the database at uri
with the name if dbname
12 13 14 15 16 |
# File 'lib/couch_object/database.rb', line 12 def self.delete!(uri, dbname) server = Server.new(uri) response = Response.new(server.delete("/#{dbname}")).parse response.parsed_body end |
.open(uri) ⇒ Object
Open a connection to the database at uri
, where uri
is a full uri like: localhost:5984/foo
29 30 31 32 33 |
# File 'lib/couch_object/database.rb', line 29 def self.open(uri) uri = URI.parse(uri) server_uri = "#{uri.scheme}://#{uri.host}:#{uri.port}" new(server_uri, uri.path.tr("/", "")) end |
Instance Method Details
#[](id) ⇒ Object
Get a document by id
87 88 89 |
# File 'lib/couch_object/database.rb', line 87 def [](id) get(id.to_s) end |
#all_documents ⇒ Object
Returns an Array of all the documents in this db
101 102 103 104 |
# File 'lib/couch_object/database.rb', line 101 def all_documents resp = Response.new(get("_all_docs")).parse resp.to_document.rows end |
#delete(path, revision) ⇒ Object
Send a DELETE request to the path
which is relative to the database path so calling with with “bar” as the path in the “foo_db” database will call host:port/foo_db/bar. Returns a Response object
81 82 83 84 |
# File 'lib/couch_object/database.rb', line 81 def delete(path, revision) Response.new(@server. delete("/#{Utils.join_url(@dbname, path)}?rev=#{revision}")).parse end |
#document(id, revision = nil) ⇒ Object
Get a document by id
, optionally a specific revision
too
92 93 94 95 96 97 98 |
# File 'lib/couch_object/database.rb', line 92 def document(id, revision=nil) if revision get("#{id}?rev=#{revision}") else get(id.to_s) end end |
#get(path) ⇒ Object
Send a GET request to the path
which is relative to the database path so calling with with “bar” as the path in the “foo_db” database will call host:port/foo_db/bar. Returns a Response object
56 57 58 |
# File 'lib/couch_object/database.rb', line 56 def get(path) Response.new(@server.get("/#{Utils.join_url(@dbname, path)}")).parse end |
#name ⇒ Object
Name of this database
48 49 50 |
# File 'lib/couch_object/database.rb', line 48 def name @dbname.dup end |
#post(path, payload, content_type = "application/json") ⇒ Object
Send a POST request to the path
which is relative to the database path so calling with with “bar” as the path in the “foo_db” database will call host:port/foo_db/bar. The post body is the payload
Returns a Response object
64 65 66 |
# File 'lib/couch_object/database.rb', line 64 def post(path, payload, content_type="application/json") Response.new(@server.post("/#{Utils.join_url(@dbname, path)}", payload, content_type)).parse end |
#put(path, payload = "") ⇒ Object
Send a PUT request to the path
which is relative to the database path so calling with with “bar” as the path in the “foo_db” database will call host:port/foo_db/bar. The put body is the payload
Returns a Response object
72 73 74 75 |
# File 'lib/couch_object/database.rb', line 72 def put(path, payload="") Response.new(@server. put("/#{Utils.join_url(@dbname, path)}", payload)).parse end |
#store(document) ⇒ Object
106 107 108 |
# File 'lib/couch_object/database.rb', line 106 def store(document) document.save(self) end |
#url ⇒ Object
The full url of this database, eg localhost:5984/foo
43 44 45 |
# File 'lib/couch_object/database.rb', line 43 def url Utils.join_url(@uri, @dbname).to_s end |