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, 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, options={})
  @url = url
  @design_docs_only = (options[:only] == :design)
end

Instance Attribute Details

#design_docs_onlyObject

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

#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



55
56
57
58
59
60
# File 'lib/couch_docs/store.rb', line 55

def self.delete(path)
  # retrieve existing to obtain the revision
  old = self.get(path)
  url = old['_rev'] ? path + "?rev=#{old['_rev']}" : path
  RestClient.delete(url)
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



62
63
64
# File 'lib/couch_docs/store.rb', line 62

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

#eachObject



66
67
68
69
70
71
72
# File 'lib/couch_docs/store.rb', line 66

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