Class: CouchDB::DataBase

Inherits:
Object
  • Object
show all
Defined in:
lib/couchdb/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, name, doc_class = Document) ⇒ DataBase

Returns a new instance of DataBase.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
# File 'lib/couchdb/database.rb', line 5

def initialize(client, name, doc_class = Document)
  raise ArgumentError, "doc_class must be a Document." unless doc_class <= Document

  @client = client
  @name = name
  @doc_class = doc_class
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/couchdb/database.rb', line 3

def name
  @name
end

Instance Method Details

#all_docs(params = nil) ⇒ Object



34
35
36
# File 'lib/couchdb/database.rb', line 34

def all_docs(params = nil)
  client.get path_for('_all_docs'), :query => params
end

#create!Object

Create or update a document.



20
21
22
# File 'lib/couchdb/database.rb', line 20

def create!
  client.put name
end

#delete(_id, _rev) ⇒ Object



76
77
78
# File 'lib/couchdb/database.rb', line 76

def delete(_id, _rev)
  client.delete path_for(_id), :query => {'rev' => _rev}
end

#delete!Object



30
31
32
# File 'lib/couchdb/database.rb', line 30

def delete!
  client.delete name
end

#ensure_exist!Object



24
25
26
27
28
# File 'lib/couchdb/database.rb', line 24

def ensure_exist!
  create!
rescue HTTPError => e
  raise e unless e.code == 419
end

#exist?(_id) ⇒ Boolean

Public: is there a document whose id is _id?

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/couchdb/database.rb', line 81

def exist?(_id)
  client.head path_for(_id)
rescue CouchDB::HTTPError
  raise $! unless $!.code == 404
end

#find(_id) ⇒ Object

Public: retrieve a document by its _id.

This method is similar to the ‘get` method, the only difference is `get` will raise CouchDB::HTTPError when CouchDB returns errors, while `find` will only return nil. Which means `get` gives you a more flexible way to handle errors.



53
54
55
56
57
# File 'lib/couchdb/database.rb', line 53

def find(_id)
  get _id
rescue HTTPError => e
  nil
end

#get(_id) ⇒ Object

Public: retrieve a document by its _id. Also see ‘find`.



43
44
45
# File 'lib/couchdb/database.rb', line 43

def get(_id)
  new_doc client.get(path_for(_id))
end

#new_doc(data) ⇒ Object



38
39
40
# File 'lib/couchdb/database.rb', line 38

def new_doc(data)
  doc_class.new self, data
end

#put(data) ⇒ Object

Public: put a hash-ish into the database.

This method can be used to create and update a document.

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/couchdb/database.rb', line 62

def put(data)
  data = new_doc data unless data.is_a?(doc_class)
  raise InvalidObject.new(data) unless data.valid?

  resp =
    if id = data.delete('_id')
      client.put path_for(id), :body => encode(data)
    else
      client.post name, :body => encode(data)
    end

  data.merge! '_id' => resp['id'], '_rev' => resp['rev']
end

#set_doc_class(doc_class) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
# File 'lib/couchdb/database.rb', line 13

def set_doc_class(doc_class)
  raise ArgumentError, "doc_class must be a Document." unless doc_class <= Document

  @doc_class = doc_class
end