Class: CouchDocs::Store

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/couch_docs/store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Store

Initialize a CouchDB store object. Requires a URL for the target CouchDB database.



14
15
16
# File 'lib/couch_docs/store.rb', line 14

def initialize(url)
  @url = url
end

Instance Attribute Details

#urlObject

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



48
49
50
51
52
# File 'lib/couch_docs/store.rb', line 48

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



37
38
39
40
# File 'lib/couch_docs/store.rb', line 37

def self.delete_and_put(path, doc)
  self.delete(path)
  self.put(path, doc)
end

.get(path) ⇒ Object



54
55
56
# File 'lib/couch_docs/store.rb', line 54

def self.get(path)
  JSON.parse(RestClient.get(path))
end

.put(path, doc) ⇒ Object



42
43
44
45
46
# File 'lib/couch_docs/store.rb', line 42

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



31
32
33
34
35
# File 'lib/couch_docs/store.rb', line 31

def self.put!(path, doc)
  self.put(path, doc)
rescue RestClient::RequestFailed
  self.delete_and_put(path, doc)
end

Instance Method Details

#eachObject



58
59
60
61
62
# File 'lib/couch_docs/store.rb', line 58

def each
  Store.get("#{url}/_all_docs")['rows'].each do |rec|
    yield Store.get("#{url}/#{rec['id']}")
  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



22
23
24
25
26
# File 'lib/couch_docs/store.rb', line 22

def put_design_documents(h)
  h.each_pair do |document_name, doc|
    Store.put!("#{url}/_design/#{document_name}", doc)
  end
end