Class: CouchPotato::Database
- Inherits:
-
Object
- Object
- CouchPotato::Database
show all
- Defined in:
- lib/couch_potato/database.rb
Defined Under Namespace
Classes: ValidationsFailedError
Instance Method Summary
collapse
Constructor Details
#initialize(couchrest_database) ⇒ Database
Returns a new instance of Database.
6
7
8
9
10
11
12
13
|
# File 'lib/couch_potato/database.rb', line 6
def initialize(couchrest_database)
@database = couchrest_database
begin
couchrest_database.info
rescue RestClient::ResourceNotFound
raise "Database '#{couchrest_database.name}' does not exist."
end
end
|
Instance Method Details
#destroy_document(document) ⇒ Object
Also known as:
destroy
38
39
40
41
42
43
44
45
|
# File 'lib/couch_potato/database.rb', line 38
def destroy_document(document)
document.run_callbacks :before_destroy
document._deleted = true
database.delete_doc document.to_hash
document.run_callbacks :after_destroy
document._id = nil
document._rev = nil
end
|
#inspect ⇒ Object
60
61
62
|
# File 'lib/couch_potato/database.rb', line 60
def inspect
"#<CouchPotato::Database>"
end
|
#load_document(id) ⇒ Object
Also known as:
load
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/couch_potato/database.rb', line 48
def load_document(id)
raise "Can't load a document without an id (got nil)" if id.nil?
begin
instance = database.get(id)
instance.database = self
instance
rescue(RestClient::ResourceNotFound)
nil
end
end
|
#save_document(document, validate = true) ⇒ Object
Also known as:
save
23
24
25
26
27
28
29
30
|
# File 'lib/couch_potato/database.rb', line 23
def save_document(document, validate = true)
return true unless document.dirty?
if document.new?
create_document(document, validate)
else
update_document(document, validate)
end
end
|
#save_document!(document) ⇒ Object
Also known as:
save!
33
34
35
|
# File 'lib/couch_potato/database.rb', line 33
def save_document!(document)
save_document(document) || raise(ValidationsFailedError.new(document.errors.full_messages))
end
|
#view(spec) ⇒ Object
15
16
17
18
19
20
|
# File 'lib/couch_potato/database.rb', line 15
def view(spec)
results = CouchPotato::View::ViewQuery.new(database,
spec.design_document, spec.view_name, spec.map_function,
spec.reduce_function).query_view!(spec.view_parameters)
spec.process_results results
end
|