Skip to content

Commit

Permalink
make the couchrest database available from the couch potato database
Browse files Browse the repository at this point in the history
  • Loading branch information
langalex committed Apr 18, 2011
1 parent 32718c7 commit 24bf5b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions lib/couch_potato/database.rb
Expand Up @@ -4,7 +4,7 @@ class Database
class ValidationsFailedError < ::StandardError; end

def initialize(couchrest_database)
@database = couchrest_database
@couchrest_database = couchrest_database
begin
couchrest_database.info
rescue RestClient::ResourceNotFound
Expand Down Expand Up @@ -45,7 +45,7 @@ def initialize(couchrest_database)
# db.view(User.all(keys: [1, 2, 3]))
def view(spec)
results = CouchPotato::View::ViewQuery.new(
database,
couchrest_database,
spec.design_document,
{spec.view_name => {
:map => spec.map_function,
Expand Down Expand Up @@ -81,7 +81,7 @@ def save_document!(document)
def destroy_document(document)
document.run_callbacks :destroy do
document._deleted = true
database.delete_doc document.to_hash
couchrest_database.delete_doc document.to_hash
end
document._id = nil
document._rev = nil
Expand All @@ -92,7 +92,7 @@ def destroy_document(document)
def load_document(id)
raise "Can't load a document without an id (got nil)" if id.nil?
begin
instance = database.get(id)
instance = couchrest_database.get(id)
instance.database = self
instance
rescue(RestClient::ResourceNotFound)
Expand All @@ -102,7 +102,12 @@ def load_document(id)
alias_method :load, :load_document

def inspect #:nodoc:
"#<CouchPotato::Database @root=\"#{database.root}\">"
"#<CouchPotato::Database @root=\"#{couchrest_database.root}\">"
end

# returns the underlying CouchRest::Database instance
def couchrest_database
@couchrest_database
end

private
Expand All @@ -121,7 +126,7 @@ def create_document(document, validate)

document.run_callbacks :save do
document.run_callbacks :create do
res = database.save_doc document.to_hash
res = couchrest_database.save_doc document.to_hash
document._rev = res['rev']
document._id = res['id']
end
Expand All @@ -141,7 +146,7 @@ def update_document(document, validate)

document.run_callbacks :save do
document.run_callbacks :update do
res = database.save_doc document.to_hash
res = couchrest_database.save_doc document.to_hash
document._rev = res['rev']
end
end
Expand All @@ -156,10 +161,5 @@ def valid_document?(document)
end
document.errors.empty?
end

def database
@database
end

end
end
2 changes: 1 addition & 1 deletion spec/property_spec.rb
Expand Up @@ -124,7 +124,7 @@ def it_should_persist value
db.should_receive(:save_doc).with do |attributes|
attributes.has_key?(:ship_address).should == true
end.and_return({})
CouchPotato.database.stub(:database).and_return(db)
CouchPotato.database.stub(:couchrest_database).and_return(db)
CouchPotato.database.save_document! p
end

Expand Down

0 comments on commit 24bf5b7

Please sign in to comment.