Class: Throne::Database
- Inherits:
-
Object
- Object
- Throne::Database
- Defined in:
- lib/throne/database.rb
Overview
Represents a connection to the database. Can be used standalone to talk to a couch instance manually, otherwise is used by the mapper.
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#create_database ⇒ Object
Creates this database, will not error if the database exists.
-
#delete(doc) ⇒ Object
deletes a document.
-
#delete_database ⇒ Object
deletes this database.
-
#function(path, params = {}) ⇒ Object
runs a function by path, returning an array of results.
-
#function_iter(path, params = {}, &block) ⇒ Object
runs a function by path, invoking once for each item.
-
#get(docid, rev = nil) ⇒ Hash?
gets a document by it’s ID.
-
#initialize(url, autocreate = true) ⇒ Database
constructor
Create a new instance, with the URL for the database to work with By default will create the database if it doesn’t exist, pass false as the second parameter to disable this.
-
#save(doc) ⇒ Object
creates/updates a document from a hash/array structure.
Constructor Details
#initialize(url, autocreate = true) ⇒ Database
Create a new instance, with the URL for the database to work with By default will create the database if it doesn’t exist, pass false as the second parameter to disable this.
11 12 13 14 |
# File 'lib/throne/database.rb', line 11 def initialize(url, autocreate = true) @url = url create_database if autocreate end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
6 7 8 |
# File 'lib/throne/database.rb', line 6 def url @url end |
Instance Method Details
#create_database ⇒ Object
Creates this database, will not error if the database exists
17 18 19 20 21 22 23 24 25 |
# File 'lib/throne/database.rb', line 17 def create_database begin C.put @url, {} rescue RestClient::RequestFailed => e unless e. =~ /412$/ raise e end end end |
#delete(doc) ⇒ Object
deletes a document. Can take an object or id
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/throne/database.rb', line 62 def delete(doc) if doc.kind_of? String rev = get(doc)['_rev'] else rev = doc['_rev'] doc = doc['_id'] end C.delete(@url + '/' + doc + '?rev=' + rev) end |
#delete_database ⇒ Object
deletes this database.
28 29 30 31 32 33 |
# File 'lib/throne/database.rb', line 28 def delete_database begin C.delete @url rescue RestClient::ResourceNotFound end end |
#function(path, params = {}) ⇒ Object
runs a function by path, returning an array of results.
74 75 76 77 78 |
# File 'lib/throne/database.rb', line 74 def function(path, params = {}) items = [] function_iter(path, params) {|i| items << i} items end |
#function_iter(path, params = {}, &block) ⇒ Object
runs a function by path, invoking once for each item.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/throne/database.rb', line 82 def function_iter(path, params = {}, &block) url = @url + '/' + path res = JP.parse(C.get(paramify_url(url, params))) res = Throne::ArrayWithFunctionMeta.new(res['rows'], res['offset']) if block_given? # TODO - stream properly, need to get objects. res.each do |i| yield Hashie::Mash.new(i) end nil else Hashie::Mash.new(res) end end |
#get(docid, rev = nil) ⇒ Hash?
gets a document by it’s ID
40 41 42 43 44 45 46 47 |
# File 'lib/throne/database.rb', line 40 def get(docid, rev=nil) begin revurl = rev ? "?rev=#{rev}" : "" Hashie::Mash.new(JP.parse(C.get(@url + '/' + docid + revurl))) rescue RestClient::ResourceNotFound nil end end |
#save(doc) ⇒ Object
creates/updates a document from a hash/array structure
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/throne/database.rb', line 50 def save(doc) if id = doc['_id'] res = C.put(@url + '/' + id, JE.encode(doc)) else res = C.post(@url, JE.encode(doc)) end res = JP.parse(res) return nil unless res['ok'] Throne::StringWithRevision.new(res['id'], res['rev']) end |