Class: Relaxo::Database

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#connectionObject (readonly)

Returns the value of attribute connection.



116
117
118
# File 'lib/relaxo/database.rb', line 116

def connection
  @connection
end

#nameObject (readonly)

Returns the value of attribute name.



117
118
119
# File 'lib/relaxo/database.rb', line 117

def name
  @name
end

#rootObject (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

#infoObject



157
158
159
# File 'lib/relaxo/database.rb', line 157

def info
	Client.get @root
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::encode_attachments!(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