Class: Couchy::Database
- Inherits:
-
Object
- Object
- Couchy::Database
- Defined in:
- lib/couchy/database.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#server ⇒ Object
Returns the value of attribute server.
Instance Method Summary collapse
-
#bulk_save(docs) ⇒ Hash
Saves or updates a bunch of documents.
-
#delete(document, revision = nil) ⇒ Hash
Deletes a document.
-
#delete! ⇒ Hash
Deletes the current database.
-
#documents(params = {}) ⇒ Hash
Gets a list of all the documents available in the database.
-
#fetch_attachment(document_id, attachment_name) ⇒ Hash
Retrieves an attachment from a document.
-
#get(id) ⇒ Hash
Retrieves a document by its ID.
-
#initialize(server, database_name) ⇒ Database
constructor
A new instance of Database.
-
#save(doc) ⇒ Hash
Saves or updates a document.
-
#temp_view(function, params = {}) ⇒ Hash
Creates a temporary view.
-
#view(view_name, params = {}) ⇒ Hash
Query a view.
Constructor Details
#initialize(server, database_name) ⇒ Database
Returns a new instance of Database.
8 9 10 11 |
# File 'lib/couchy/database.rb', line 8 def initialize(server, database_name) @name = database_name @server = server end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/couchy/database.rb', line 6 def name @name end |
#server ⇒ Object
Returns the value of attribute server.
6 7 8 |
# File 'lib/couchy/database.rb', line 6 def server @server end |
Instance Method Details
#bulk_save(docs) ⇒ Hash
Saves or updates a bunch of documents
82 83 84 |
# File 'lib/couchy/database.rb', line 82 def bulk_save(docs) server.post("#{name}/_bulk_docs", {:docs => docs}) end |
#delete(document, revision = nil) ⇒ Hash
Deletes a document
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/couchy/database.rb', line 98 def delete(document, revision=nil) case document when String raise ArgumentError, 'Document revision must be specified' unless revision server.delete "#{name}/#{CGI.escape(document)}", :rev => revision when Hash raise ArgumentError, 'Document ID and revision must be specified' unless document['_id'] && document['_rev'] server.delete("#{name}/#{CGI.escape(document['_id'])}", :rev => document['_rev']) else raise ArgumentError, 'Document must be an Hash representing the document or its ID' end end |
#delete! ⇒ Hash
Deletes the current database
115 116 117 |
# File 'lib/couchy/database.rb', line 115 def delete! server.delete(name) end |
#documents(params = {}) ⇒ Hash
Gets a list of all the documents available in the database
18 19 20 |
# File 'lib/couchy/database.rb', line 18 def documents(params={}) server.get("#{name}/_all_docs", params) end |
#fetch_attachment(document_id, attachment_name) ⇒ Hash
Retrieves an attachment from a document
58 59 60 |
# File 'lib/couchy/database.rb', line 58 def (document_id, ) server.get("#{name}/#{CGI.escape(document_id)}/#{CGI.escape(attachment_name)}", :no_json => true) end |
#get(id) ⇒ Hash
Retrieves a document by its ID
48 49 50 |
# File 'lib/couchy/database.rb', line 48 def get(id) server.get("#{name}/#{CGI.escape(id)}") end |
#save(doc) ⇒ Hash
Saves or updates a document
67 68 69 70 71 72 73 74 75 |
# File 'lib/couchy/database.rb', line 67 def save(doc) doc = (doc) if doc['_id'] server.put("#{name}/#{CGI.escape(doc['_id'])}", doc) else server.post("#{name}", doc) end end |
#temp_view(function, params = {}) ⇒ Hash
Creates a temporary view
28 29 30 31 |
# File 'lib/couchy/database.rb', line 28 def temp_view(function, params={}) server.post("#{name}/_temp_view", function, params.merge!(:headers => {'Content-Type' => 'application/json'})) end |
#view(view_name, params = {}) ⇒ Hash
Query a view
39 40 41 |
# File 'lib/couchy/database.rb', line 39 def view(view_name, params={}) server.get("#{name}/_view/#{view_name}", params) end |