Class: Smeg2::Database
- Inherits:
-
Object
- Object
- Smeg2::Database
- Defined in:
- lib/smeg2/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.
-
#design(path, params = {}, &block) ⇒ Object
runs a design by path, with optional params passed in.
-
#get(docid, rev = nil) ⇒ Object
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/smeg2/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/smeg2/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/smeg2/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
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/smeg2/database.rb', line 54 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/smeg2/database.rb', line 28 def delete_database begin c.delete @url rescue RestClient::ResourceNotFound end end |
#design(path, params = {}, &block) ⇒ Object
runs a design by path, with optional params passed in
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/smeg2/database.rb', line 66 def design(path, params = {}, &block) url = @url + '/_design/' + path res = JSON.parse(c.get(paramify_url(url, params))) if block_given? # TODO - stream properly res.each do |i| yield i end nil else res end end |
#get(docid, rev = nil) ⇒ Object
gets a document by it’s ID
36 37 38 39 |
# File 'lib/smeg2/database.rb', line 36 def get(docid, rev=nil) revurl = rev ? "?rev=#{rev}" : "" JSON.parse(c.get(@url + '/' + docid + revurl)) end |
#save(doc) ⇒ Object
creates/updates a document from a hash/array structure
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/smeg2/database.rb', line 42 def save(doc) if id = doc['_id'] res = c.put(@url + '/' + id, doc.to_json) else res = c.post(@url, doc.to_json) end res = JSON.parse(res) return nil unless res['ok'] Smeg2::StringWithRevision.new(res['id'], res['rev']) end |