Class: CouchRest::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/couchrest/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.


6
7
8
9
# File 'lib/couchrest/server.rb', line 6

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


15
16
17
# File 'lib/couchrest/server.rb', line 15

def available_databases
  @available_databases
end

#uriObject

Returns the value of attribute uri


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

def uri
  @uri
end

#uuid_batch_countObject

Returns the value of attribute uuid_batch_count


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

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)

36
37
38
# File 'lib/couchrest/server.rb', line 36

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


72
73
74
75
# File 'lib/couchrest/server.rb', line 72

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

#database(name) ⇒ Object

Returns a CouchRest::Database for the given name


54
55
56
# File 'lib/couchrest/server.rb', line 54

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

#database!(name) ⇒ Object

Creates the database if it doesn't exist


59
60
61
62
63
64
# File 'lib/couchrest/server.rb', line 59

def database!(name)
  CouchRest.head "#{@uri}/#{name}" # Check if the URL is valid
  database(name)
rescue RestClient::ResourceNotFound # Thrown if the HTTP HEAD fails
  create_db(name)
end

#databasesObject

Lists all databases on the server


49
50
51
# File 'lib/couchrest/server.rb', line 49

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

#default_databaseObject


44
45
46
# File 'lib/couchrest/server.rb', line 44

def default_database
  available_databases[:default]
end

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


40
41
42
# File 'lib/couchrest/server.rb', line 40

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


26
27
28
# File 'lib/couchrest/server.rb', line 26

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


67
68
69
# File 'lib/couchrest/server.rb', line 67

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.


83
84
85
86
87
88
89
# File 'lib/couchrest/server.rb', line 83

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


78
79
80
# File 'lib/couchrest/server.rb', line 78

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