Class: CouchModel::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_model/database.rb

Overview

The Database class provides methods create, delete and retrieve informations of a CouchDB database.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = { }) ⇒ Database

Returns a new instance of Database.



14
15
16
17
# File 'lib/couch_model/database.rb', line 14

def initialize(options = { })
  @name   = options[:name]    || raise(ArgumentError, "no database was given")
  @server = options[:server]  || Server.new
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/couch_model/database.rb', line 12

def name
  @name
end

#serverObject (readonly)

Returns the value of attribute server.



11
12
13
# File 'lib/couch_model/database.rb', line 11

def server
  @server
end

Instance Method Details

#==(other) ⇒ Object



19
20
21
# File 'lib/couch_model/database.rb', line 19

def ==(other)
  other.is_a?(self.class) && @name == other.name && @server == other.server
end

#===(other) ⇒ Object



23
24
25
# File 'lib/couch_model/database.rb', line 23

def ===(other)
  object_id == other.object_id
end

#create!Object



27
28
29
# File 'lib/couch_model/database.rb', line 27

def create!
  Transport::JSON.request :put, url, :expected_status_code => 201
end

#create_if_missing!Object



31
32
33
# File 'lib/couch_model/database.rb', line 31

def create_if_missing!
  create! unless exists?
end

#delete!Object



35
36
37
# File 'lib/couch_model/database.rb', line 35

def delete!
  Transport::JSON.request :delete, url, :expected_status_code => 200
end

#delete_if_exists!Object



39
40
41
# File 'lib/couch_model/database.rb', line 39

def delete_if_exists!
  delete! if exists?
end

#documents(options = { }) ⇒ Object



55
56
57
# File 'lib/couch_model/database.rb', line 55

def documents(options = { })
  Collection.new url + "/_all_docs", options
end

#exists?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/couch_model/database.rb', line 47

def exists?
  @server.database_names.include? @name
end

#informationsObject



43
44
45
# File 'lib/couch_model/database.rb', line 43

def informations
  Transport::JSON.request :get, url, :expected_status_code => 200
end

#urlObject



51
52
53
# File 'lib/couch_model/database.rb', line 51

def url
  "#{@server.url}/#{@name}"
end