Class: CouchDocs::Store
- Inherits:
-
Object
- Object
- CouchDocs::Store
- Includes:
- Enumerable
- Defined in:
- lib/couch_docs/store.rb
Instance Attribute Summary collapse
-
#design_docs_only ⇒ Object
Returns the value of attribute design_docs_only.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .delete(path) ⇒ Object
- .delete_and_put(path, doc) ⇒ Object
- .get(path) ⇒ Object
- .post(path, doc) ⇒ Object
- .put(path, doc) ⇒ Object
-
.put!(path, doc) ⇒ Object
Create or replace the document located at
path
with the Hash documentdoc
.
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(url, options = {}) ⇒ Store
constructor
Initialize a CouchDB store object.
-
#put_design_documents(h) ⇒ Object
Loads all supplied design documents in the current store.
Constructor Details
#initialize(url, options = {}) ⇒ Store
Initialize a CouchDB store object. Requires a URL for the target CouchDB database.
14 15 16 17 |
# File 'lib/couch_docs/store.rb', line 14 def initialize(url, ={}) @url = url @design_docs_only = ([:only] == :design) end |
Instance Attribute Details
#design_docs_only ⇒ Object
Returns the value of attribute design_docs_only.
9 10 11 |
# File 'lib/couch_docs/store.rb', line 9 def design_docs_only @design_docs_only end |
#url ⇒ Object
Returns the value of attribute url.
9 10 11 |
# File 'lib/couch_docs/store.rb', line 9 def url @url end |
Class Method Details
.delete(path) ⇒ Object
55 56 57 58 59 |
# File 'lib/couch_docs/store.rb', line 55 def self.delete(path) # retrieve existing to obtain the revision old = self.get(path) RestClient.delete(path + "?rev=#{old['_rev']}") end |
.delete_and_put(path, doc) ⇒ Object
38 39 40 41 |
# File 'lib/couch_docs/store.rb', line 38 def self.delete_and_put(path, doc) self.delete(path) self.put(path, doc) end |
.get(path) ⇒ Object
61 62 63 |
# File 'lib/couch_docs/store.rb', line 61 def self.get(path) JSON.parse(RestClient.get(path)) end |
.post(path, doc) ⇒ Object
49 50 51 52 53 |
# File 'lib/couch_docs/store.rb', line 49 def self.post(path, doc) RestClient.post path, doc.to_json, :content_type => 'application/json' end |
.put(path, doc) ⇒ Object
43 44 45 46 47 |
# File 'lib/couch_docs/store.rb', line 43 def self.put(path, doc) RestClient.put path, doc.to_json, :content_type => 'application/json' end |
.put!(path, doc) ⇒ Object
Create or replace the document located at path
with the Hash document doc
32 33 34 35 36 |
# File 'lib/couch_docs/store.rb', line 32 def self.put!(path, doc) self.put(path, doc) rescue RestClient::RequestFailed self.delete_and_put(path, doc) end |
Instance Method Details
#each ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/couch_docs/store.rb', line 65 def each all_url = "#{url}/_all_docs" + (design_docs_only ? '?startkey=%22_design%22&endkey=%22_design0%22' : "") Store.get(all_url)['rows'].each do |rec| yield Store.get("#{url}/#{rec['id']}?attachments=true") end end |
#put_design_documents(h) ⇒ Object
Loads all supplied design documents in the current store. Given a hash h
, the keys being the CouchDB document name and values of design documents
23 24 25 26 27 |
# File 'lib/couch_docs/store.rb', line 23 def put_design_documents(h) h.each_pair do |document_name, doc| Store.put!("#{url}/_design/#{document_name}", doc) end end |