Class: WingedCouch::Native::Database

Inherits:
Abstract::Database show all
Includes:
WingedCouch::Native::Databases::Bulk, WingedCouch::Native::Databases::Operations, WingedCouch::Native::Databases::Sugar
Defined in:
lib/winged_couch/native/database.rb

Overview

Low-level class for managing databases in CouchDB

Constant Summary collapse

RESERVED_DATABASES =
["_users", "_replicator"]

Instance Attribute Summary

Attributes inherited from Abstract::Database

#name

Instance Method Summary collapse

Methods included from WingedCouch::Native::Databases::Operations

#changes, #compact, #compact_doc, #documents_count, #ensure_full_commit, #info, #view_cleanup

Methods included from WingedCouch::Native::Databases::Bulk

#bulk, #bulk_path

Methods included from WingedCouch::Native::Databases::Sugar

included

Methods inherited from Abstract::Database

#==, #initialize, #inspect

Constructor Details

This class inherits a constructor from WingedCouch::Abstract::Database

Instance Method Details

#createObject

Creates database

Raises:


61
62
63
64
# File 'lib/winged_couch/native/database.rb', line 61

def create
  HTTP.put path
  self
end

#design_docsObject


98
99
100
# File 'lib/winged_couch/native/database.rb', line 98

def design_docs
  HTTP.get path.join('_all_docs?startkey=%22_design%2F%22&endkey=%22_design0%22&include_docs=true')
end

#design_documentObject

Returns default WingedCouch design document from current database

Returns:

  • WingedCouch::Design::Document


78
79
80
81
82
83
84
# File 'lib/winged_couch/native/database.rb', line 78

def design_document
  @design_document ||= begin
    doc = Design::Document.new(self)
    doc.exist? ? doc.reload : doc.save
    doc
  end
end

#droptrue

Drops the database

Examples:

db = WingedCouch::Native::Database.create("my_db")
# => #<WingedCouch::Native::Database name='my_db'>
db.drop
# => true

Returns:

  • (true)

Raises:


33
34
35
36
37
38
# File 'lib/winged_couch/native/database.rb', line 33

def drop
  check_database_name(name)
  HTTP.delete path
  @design_document = nil
  true
end

#exist?true, false

Returns true if database exist in CouchDB

Examples:

db = WingedCouch::Native::Database.new("my_db")
# => #<WingedCouch::Native::Database name='my_db'>
db.exist?
# => false

Returns:

  • (true, false)

50
51
52
53
54
55
# File 'lib/winged_couch/native/database.rb', line 50

def exist?
  HTTP.head path
  true
rescue Exceptions::NoDatabase
  false
end

#pathWingedCouch::HttpPath

Returns HTTP path to database


70
71
72
# File 'lib/winged_couch/native/database.rb', line 70

def path
  HTTP.path.join(name, :database)
end

#truncateObject

Truncates current database (actually, drops it and re-creates)


88
89
90
91
92
93
94
95
96
# File 'lib/winged_couch/native/database.rb', line 88

def truncate
  begin
    drop
  rescue Exceptions::ReservedDatabase
    nil
  rescue Exceptions::NoDatabase
    create
  end
end