Class: CouchPotato::Database

Inherits:
Object
  • Object
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
# File 'lib/couch_potato/database.rb', line 6

def initialize(couchrest_database)
  @database = couchrest_database
end

Instance Method Details

#destroy_document(document) ⇒ Object Also known as: destroy



32
33
34
35
36
37
38
39
# File 'lib/couch_potato/database.rb', line 32

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

#inspectObject



52
53
54
# File 'lib/couch_potato/database.rb', line 52

def inspect
  "#<CouchPotato::Database>"
end

#load_document(id) ⇒ Object Also known as: load



42
43
44
45
46
47
48
49
# File 'lib/couch_potato/database.rb', line 42

def load_document(id)
  begin
    json = database.get(id)
    Class.const_get(json['ruby_class']).json_create json
  rescue(RestClient::ResourceNotFound)
    nil
  end
end

#save_document(document) ⇒ Object Also known as: save



17
18
19
20
21
22
23
24
# File 'lib/couch_potato/database.rb', line 17

def save_document(document)
  return true unless document.dirty?
  if document.new?
    create_document document
  else
    update_document document
  end
end

#save_document!(document) ⇒ Object Also known as: save!



27
28
29
# File 'lib/couch_potato/database.rb', line 27

def save_document!(document)
  save_document(document) || raise(ValidationsFailedError.new(document.errors.full_messages))
end

#view(spec) ⇒ Object



10
11
12
13
14
15
# File 'lib/couch_potato/database.rb', line 10

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