Module: CouchRest::Model::Connection::ClassMethods

Defined in:
lib/couchrest/model/connection.rb

Instance Method Summary collapse

Instance Method Details

#databaseObject

Overwrite the default database method so that it always provides something from the configuration. It will try to inherit the database from an ancester unless the use_database method has been used, in which case a new connection will be started.



25
26
27
# File 'lib/couchrest/model/connection.rb', line 25

def database
  @database ||= prepare_database(super)
end

#prepare_database(db = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/couchrest/model/connection.rb', line 33

def prepare_database(db = nil)
  db = @_use_database unless @_use_database.nil?
  if db.nil? || db.is_a?(String) || db.is_a?(Symbol)
    conf = connection_configuration
    db = [conf[:prefix], db.to_s, conf[:suffix]].reject{|s| s.to_s.empty?}.join(conf[:join])
    self.server.database!(db)
  else
    db
  end
end

#serverObject



29
30
31
# File 'lib/couchrest/model/connection.rb', line 29

def server
  @server ||= CouchRest::Server.new(prepare_server_uri)
end

#use_database(db) ⇒ Object

Overwrite the normal use_database method so that a database name can be provided instead of a full connection. The actual database will be validated when it is requested for use. Note that this should not be used with proxied models!



16
17
18
# File 'lib/couchrest/model/connection.rb', line 16

def use_database(db)
  @_use_database = db
end