Class: CouchRest::Server

Inherits:
Object show all
Defined in:
lib/couchrest/core/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server = 'http://127.0.0.1:5984', uuid_batch_count = 1000) ⇒ Server

Returns a new instance of Server.



4
5
6
7
# File 'lib/couchrest/core/server.rb', line 4

def initialize(server = 'http://127.0.0.1:5984', uuid_batch_count = 1000)
  @uri = server
  @uuid_batch_count = uuid_batch_count
end

Instance Attribute Details

#available_databasesObject

Lists all “available” databases. An available database, is a database that was specified as avaiable by your code. It allows to define common databases to use and reuse in your code



13
14
15
# File 'lib/couchrest/core/server.rb', line 13

def available_databases
  @available_databases
end

#uriObject

Returns the value of attribute uri.



3
4
5
# File 'lib/couchrest/core/server.rb', line 3

def uri
  @uri
end

#uuid_batch_countObject

Returns the value of attribute uuid_batch_count.



3
4
5
# File 'lib/couchrest/core/server.rb', line 3

def uuid_batch_count
  @uuid_batch_count
end

Instance Method Details

#available_database?(ref_or_name) ⇒ Boolean

Checks that a database is set as available

Example:

@couch.available_database?(:default)

Returns:

  • (Boolean)


34
35
36
# File 'lib/couchrest/core/server.rb', line 34

def available_database?(ref_or_name)
  ref_or_name.is_a?(Symbol) ? available_databases.keys.include?(ref_or_name) : available_databases.values.map{|db| db.name}.include?(ref_or_name)
end

#create_db(name) ⇒ Object

Create a database



68
69
70
71
# File 'lib/couchrest/core/server.rb', line 68

def create_db(name)
  CouchRest.put "#{@uri}/#{name}"
  database(name)
end

#database(name) ⇒ Object

Returns a CouchRest::Database for the given name



52
53
54
# File 'lib/couchrest/core/server.rb', line 52

def database(name)
  CouchRest::Database.new(self, name)
end

#database!(name) ⇒ Object

Creates the database if it doesn’t exist



57
58
59
60
# File 'lib/couchrest/core/server.rb', line 57

def database!(name)
  create_db(name) rescue nil
  database(name)
end

#databasesObject

Lists all databases on the server



47
48
49
# File 'lib/couchrest/core/server.rb', line 47

def databases
  CouchRest.get "#{@uri}/_all_dbs"
end

#default_databaseObject



42
43
44
# File 'lib/couchrest/core/server.rb', line 42

def default_database
  available_databases[:default]
end

#default_database=(name, create_unless_exists = true) ⇒ Object



38
39
40
# File 'lib/couchrest/core/server.rb', line 38

def default_database=(name, create_unless_exists = true)
  define_available_database(:default, name, create_unless_exists = true)
end

#define_available_database(reference, db_name, create_unless_exists = true) ⇒ Object

Adds a new available database and create it unless it already exists

Example:

@couch.define_available_database(:default, “tech-blog”)



24
25
26
# File 'lib/couchrest/core/server.rb', line 24

def define_available_database(reference, db_name, create_unless_exists = true)
  available_databases[reference.to_sym] = create_unless_exists ? database!(db_name) : database(db_name)
end

#infoObject

GET the welcome message



63
64
65
# File 'lib/couchrest/core/server.rb', line 63

def info
  CouchRest.get "#{@uri}/"
end

#next_uuid(count = @uuid_batch_count) ⇒ Object

Retrive an unused UUID from CouchDB. Server instances manage caching a list of unused UUIDs.



79
80
81
82
83
84
85
# File 'lib/couchrest/core/server.rb', line 79

def next_uuid(count = @uuid_batch_count)
  @uuids ||= []
  if @uuids.empty?
    @uuids = CouchRest.get("#{@uri}/_uuids?count=#{count}")["uuids"]
  end
  @uuids.pop
end

#restart!Object

Restart the CouchDB instance



74
75
76
# File 'lib/couchrest/core/server.rb', line 74

def restart!
  CouchRest.post "#{@uri}/_restart"
end