Class: Relaxo::Database
- Inherits:
-
Object
- Object
- Relaxo::Database
- Defined in:
- lib/relaxo/session.rb,
lib/relaxo/database.rb
Overview
We monkey-patch this in as Session is basically an optional feature for doing bulk_saves.
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #bulk_save(documents) ⇒ Object
- #delete(document) ⇒ Object
- #documents(parameters = {}) ⇒ Object
- #get(id, parameters = {}) ⇒ Object
- #info ⇒ Object
-
#initialize(connection, name) ⇒ Database
constructor
A new instance of Database.
- #put(document) ⇒ Object
- #save(document) ⇒ Object
- #session(klass = nil) ⇒ Object
-
#view(name, parameters = {}) ⇒ Object
Accepts paramaters as described in wiki.apache.org/couchdb/HttpViewApi.
Constructor Details
#initialize(connection, name) ⇒ Database
Returns a new instance of Database.
109 110 111 112 113 114 |
# File 'lib/relaxo/database.rb', line 109 def initialize(connection, name) @connection = connection @name = name @root = connection.url + "/" + CGI.escape(name) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
116 117 118 |
# File 'lib/relaxo/database.rb', line 116 def connection @connection end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
117 118 119 |
# File 'lib/relaxo/database.rb', line 117 def name @name end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
118 119 120 |
# File 'lib/relaxo/database.rb', line 118 def root @root end |
Instance Method Details
#bulk_save(documents) ⇒ Object
145 146 147 148 149 150 |
# File 'lib/relaxo/database.rb', line 145 def bulk_save(documents) Client.post command_url("_bulk_docs"), { :docs => documents, :all_or_nothing => true } end |
#delete(document) ⇒ Object
128 129 130 |
# File 'lib/relaxo/database.rb', line 128 def delete(document) Client.delete document_url(document[ID]) + "?rev=#{document[REV]}" end |
#documents(parameters = {}) ⇒ Object
161 162 163 |
# File 'lib/relaxo/database.rb', line 161 def documents(parameters = {}) view("_all_docs", parameters) end |
#get(id, parameters = {}) ⇒ Object
120 121 122 |
# File 'lib/relaxo/database.rb', line 120 def get(id, parameters = {}) Client.get document_url(id, parameters) end |
#put(document) ⇒ Object
124 125 126 |
# File 'lib/relaxo/database.rb', line 124 def put(document) Client.put document_url(document[ID] || @connection.next_uuid), document end |
#save(document) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/relaxo/database.rb', line 132 def save(document) Relaxo::(document) status = put(document) if status['ok'] document[ID] = status['id'] document[REV] = status['rev'] end return status end |
#session(klass = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/relaxo/session.rb', line 28 def session(klass = nil) session = Session.new(self) catch(:abort) do yield session changed = session.commit! if klass klass.new(self, changed.last) else changed end end end |
#view(name, parameters = {}) ⇒ Object
Accepts paramaters as described in wiki.apache.org/couchdb/HttpViewApi
153 154 155 |
# File 'lib/relaxo/database.rb', line 153 def view(name, parameters = {}) Client.get view_url(name, parameters) end |