Class: Divan::Database

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Database

Returns a new instance of Database.



6
7
8
9
10
11
12
13
14
# File 'lib/divan/database.rb', line 6

def initialize(name, options = {})
  @name, @user, @password = name, options['user'], options['password']
#TODO: Add user & password support
  @host     = options['host'] || 'http://127.0.0.1'
  @port     = options['port'] || 5984
  @database = options['database']
  @views    = {}
  build_model_class
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



5
6
7
# File 'lib/divan/database.rb', line 5

def database
  @database
end

#hostObject

Returns the value of attribute host.



5
6
7
# File 'lib/divan/database.rb', line 5

def host
  @host
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/divan/database.rb', line 5

def name
  @name
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'lib/divan/database.rb', line 5

def password
  @password
end

#portObject

Returns the value of attribute port.



5
6
7
# File 'lib/divan/database.rb', line 5

def port
  @port
end

#userObject

Returns the value of attribute user.



5
6
7
# File 'lib/divan/database.rb', line 5

def user
  @user
end

#viewsObject

Returns the value of attribute views.



5
6
7
# File 'lib/divan/database.rb', line 5

def views
  @views
end

Instance Method Details

#[](path, params = {}) ⇒ Object



72
73
74
# File 'lib/divan/database.rb', line 72

def [](path, params={})
  client[ Divan::Utils.formatted_path(path, params) ]
end

#clientObject



76
77
78
# File 'lib/divan/database.rb', line 76

def client
  @client ||= RestClient::Resource.new( basic_url, *([@user, @password].compact) )
end

#compactObject



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

def compact
  begin
    client['_compact'].post Hash.new, :'content-type' => 'application/json'
  rescue RestClient::ResourceNotFound
    raise Divan::DatabaseNotFound.new(self), "Database was not found"
  end
end

#createObject



41
42
43
44
45
46
47
# File 'lib/divan/database.rb', line 41

def create
  begin
    client.put Hash.new
  rescue RestClient::PreconditionFailed
    raise Divan::DatabaseAlreadyCreated.new(self), "Database already created"
  end
end

#create_view(view_name) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/divan/database.rb', line 63

def create_view(view_name)
  if view_doc = model_class.find("_design/#{view_name}")
    view_doc.views = views[view_name]
    view_doc.save
  else
    model_class.create :id => "_design/#{view_name}", :language => 'javascript', :views => views[view_name]
  end
end

#create_viewsObject



57
58
59
60
61
# File 'lib/divan/database.rb', line 57

def create_views
  views.each do |name, views|
    create_view(name)
  end
end

#deleteObject



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

def delete
  begin
    client.delete
  rescue RestClient::ResourceNotFound
    raise Divan::DatabaseNotFound.new(self), "Database was not found"
  end
end

#exists?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
# File 'lib/divan/database.rb', line 16

def exists?
  begin
    client.get
    return true
  rescue RestClient::ResourceNotFound
    return false
  end
end

#model_classObject



80
81
82
# File 'lib/divan/database.rb', line 80

def model_class
  @model_class ||= eval model_class_full_name
end

#statsObject



25
26
27
28
29
30
31
# File 'lib/divan/database.rb', line 25

def stats
  begin
    JSON.parse client.get, :symbolize_names => true
  rescue RestClient::ResourceNotFound
    raise Divan::DatabaseNotFound.new(self), "Database was not found"
  end
end