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
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



72
73
74
75
76
77
78
79
# File 'lib/couch_potato/database.rb', line 72

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

:nodoc:



95
96
97
# File 'lib/couch_potato/database.rb', line 95

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

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

loads a document by its id



83
84
85
86
87
88
89
90
91
92
# File 'lib/couch_potato/database.rb', line 83

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

saves a document. returns true on success, false on failure



56
57
58
59
60
61
62
63
# File 'lib/couch_potato/database.rb', line 56

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!

saves a document, raises a CouchPotato::Database::ValidationsFailedError on failure



67
68
69
# File 'lib/couch_potato/database.rb', line 67

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

#view(spec) ⇒ Object

executes a view and return the results. you pass in a view spec which is usually a result of a SomePersistentClass.some_view call. also return the total_rows returned by CouchDB as an accessor on the results.

Example:

class User
  include CouchPotato::Persistence
  property :age
  view :all, key: :age
end
db = CouchPotato.database

db.view(User.all) # => [user1, user2]
db.view(User.all).total_rows # => 2

You can pass the usual parameters you can pass to a couchdb view to the view:

db.view(User.all(limit: 5, startkey: 2, reduce: false))

For your convenience when passing a has with only a key parameter you can just pass in the value

db.view(User.all(key: 1)) == db.view(User.all(1))

Instead of passing a startkey and endkey you can pass in a key with a range:

db.view(User.all(key: 1..20)) == db.view(startkey: 1, endkey: 20) == db.view(User.all(1..20))


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/couch_potato/database.rb', line 43

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)
  processed_results = spec.process_results results
  processed_results.instance_eval "def total_rows; #{results['total_rows']}; end" if results['total_rows']
  processed_results.each do |document|
    document.database = self if document.respond_to?(:database=)
  end if processed_results.respond_to?(:each)
  processed_results
end