Class: Divan::Database
- Inherits:
-
Object
- Object
- Divan::Database
- Defined in:
- lib/divan/database.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
Returns the value of attribute database.
-
#host ⇒ Object
Returns the value of attribute host.
-
#name ⇒ Object
Returns the value of attribute name.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#user ⇒ Object
Returns the value of attribute user.
-
#views ⇒ Object
Returns the value of attribute views.
Instance Method Summary collapse
- #[](path, params = {}) ⇒ Object
- #client ⇒ Object
- #compact ⇒ Object
- #create ⇒ Object
- #create_view(view_name) ⇒ Object
- #create_views ⇒ Object
- #delete ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(name, options = {}) ⇒ Database
constructor
A new instance of Database.
- #model_class ⇒ Object
- #stats ⇒ Object
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, = {}) @name, @user, @password = name, ['user'], ['password'] #TODO: Add user & password support @host = ['host'] || 'http://127.0.0.1' @port = ['port'] || 5984 @database = ['database'] @views = {} build_model_class end |
Instance Attribute Details
#database ⇒ Object
Returns the value of attribute database.
5 6 7 |
# File 'lib/divan/database.rb', line 5 def database @database end |
#host ⇒ Object
Returns the value of attribute host.
5 6 7 |
# File 'lib/divan/database.rb', line 5 def host @host end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/divan/database.rb', line 5 def name @name end |
#password ⇒ Object
Returns the value of attribute password.
5 6 7 |
# File 'lib/divan/database.rb', line 5 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
5 6 7 |
# File 'lib/divan/database.rb', line 5 def port @port end |
#user ⇒ Object
Returns the value of attribute user.
5 6 7 |
# File 'lib/divan/database.rb', line 5 def user @user end |
#views ⇒ Object
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 |
#client ⇒ Object
76 77 78 |
# File 'lib/divan/database.rb', line 76 def client @client ||= RestClient::Resource.new( basic_url, *([@user, @password].compact) ) end |
#compact ⇒ Object
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 |
#create ⇒ Object
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_views ⇒ Object
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 |
#delete ⇒ Object
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
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_class ⇒ Object
80 81 82 |
# File 'lib/divan/database.rb', line 80 def model_class @model_class ||= eval model_class_full_name end |
#stats ⇒ Object
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 |