Class: ChillDB::Document
- Inherits:
-
IndifferentHash
- Object
- Hash
- IndifferentHash
- ChillDB::Document
- Defined in:
- lib/chill.rb
Overview
ChillDB Document, normally created from a template via ChillDB.template or from scratch via ChillDB.document or with a specific _id value via ChillDB or ChillDB.document(‘document-id’). Document represents a document in your database. It works as a hash with indifferent access and method accessors (see also ChillDB::IndifferentHash)
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
Class Method Summary collapse
-
.load(database, docid, revision = nil) ⇒ Object
load a documet from a ChillDB::Database, with a specific document id, and optionally a specific revision.
Instance Method Summary collapse
-
#==(other_doc) ⇒ Object
A loose equality check.
-
#commit! ⇒ Object
Write any changes to the database.
-
#delete ⇒ Object
Mark this document for deletion.
-
#delete! ⇒ Object
Shortcut for document.delete.commit! - deletes document from server immediately, returning the server’s response as a Hash.
-
#initialize(database, values = false) ⇒ Document
constructor
:nodoc:.
-
#load(lookup_revision = nil) ⇒ Object
load the current document again from the server, fetching any updates or a specific revision.
-
#reset(values) ⇒ Object
replace all values in this document with new ones, effectively making it a new document.
-
#revision ⇒ Object
get’s the current document revision identifier string.
-
#revision=(new_revision) ⇒ Object
set the current document revision, reloading document content from the couch server.
-
#revisions ⇒ Object
fetch an array of this document’s available revisions from the server.
-
#to_param ⇒ Object
to_param is supplied for integration with url routers in web frameworks like Rails and Camping.
Methods inherited from IndifferentHash
#[]=, #method_missing, #to_hash
Constructor Details
#initialize(database, values = false) ⇒ Document
:nodoc:
375 376 377 378 379 380 381 382 383 384 |
# File 'lib/chill.rb', line 375 def initialize database, values = false # :nodoc: @database = database super() if values.is_a? Symbol reset @database.class_variable_get(:@@templates)[values] elsif values reset values end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ChillDB::IndifferentHash
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
373 374 375 |
# File 'lib/chill.rb', line 373 def database @database end |
Class Method Details
.load(database, docid, revision = nil) ⇒ Object
load a documet from a ChillDB::Database, with a specific document id, and optionally a specific revision.
Returns:
New instance of Document
403 404 405 |
# File 'lib/chill.rb', line 403 def self.load database, docid, revision = nil new(database, _id: docid).load end |
Instance Method Details
#==(other_doc) ⇒ Object
A loose equality check. Two documents are considered equal if their _id and _rev are equal, and they are both of the same class. This is handy when dealing with Array’s and ChillDB::List’s. For instance, you may want to query one view, then remove the values found in another view
Example:
kittens = KittensApp.design(:lists).query(:cats)
kittens -= KittensApp.design(:lists).query(:adults)
# kittens now contains cats who are not adults
Of course if you’re doing this sort of thing often, you really should consider precomputing it in a view.
509 510 511 |
# File 'lib/chill.rb', line 509 def == other_doc (other.class == self.class) and (self['_id'] == other['_id']) and (self['_rev'] == other['_rev']) end |
#commit! ⇒ Object
Write any changes to the database. If this is a new document, one will be created on the Couch server. If this document has a specific _id value it will be created or updated.
Returns: self
428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/chill.rb', line 428 def commit! return delete! if self['_deleted'] # just delete if we're marked for deletion json = JSON.generate(self) response = @database.http(URI.escape self['_id']).put(json); raise response.body unless (200..299).include? response.code json = JSON.parse(response.body) raise "Not ok! #{response.body}" unless json['ok'] self['_id'] = json['id'] self['_rev'] = json['rev'] return self end |
#delete ⇒ Object
Mark this document for deletion. A commit! is required to delete the document from the server. You can mark several documents for deletion and supply them to ChillDB.commit! for bulk deletion.
443 444 445 |
# File 'lib/chill.rb', line 443 def delete self.replace('_id'=> self['_id'], '_rev'=> self['_rev'], '_deleted'=> true) end |
#delete! ⇒ Object
Shortcut for document.delete.commit! - deletes document from server immediately, returning the server’s response as a Hash.
449 450 451 452 453 454 455 |
# File 'lib/chill.rb', line 449 def delete! response = @database.http(URI.escape self['_id']).delete() response = ChillDB::IndifferentHash.new.replace JSON.parse(response.body) raise "Couldn't delete #{self._id}: #{response.error} - #{response.reason}" if response['error'] delete # make our contents be deleted return response end |
#load(lookup_revision = nil) ⇒ Object
load the current document again from the server, fetching any updates or a specific revision.
Returns: self
411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/chill.rb', line 411 def load lookup_revision = nil url = URI(URI.escape self['_id']) lookup_revision ||= self['_rev'] url.query = "rev=#{lookup_revision}" if lookup_revision response = @database.http(url).get return self if response.code == 404 # we can create it later raise response.body if response.code != 200 reset JSON.parse(response.body) return self end |
#reset(values) ⇒ Object
replace all values in this document with new ones, effectively making it a new document
Arguments:
values: A hash of values
391 392 393 394 395 396 |
# File 'lib/chill.rb', line 391 def reset values raise "Argument must be a Hash" unless values.respond_to? :to_hash self.replace values.to_hash self['_id'] ||= SecureRandom.uuid # generate an _id if we don't have one already self end |
#revision ⇒ Object
get’s the current document revision identifier string
Returns: (String) revision identifier
469 470 471 |
# File 'lib/chill.rb', line 469 def revision self['_rev'] end |
#revision=(new_revision) ⇒ Object
set the current document revision, reloading document content from the couch server.
Arguments:
new_revision: (String)
462 463 464 |
# File 'lib/chill.rb', line 462 def revision= new_revision load new_revision end |
#revisions ⇒ Object
fetch an array of this document’s available revisions from the server
Returns: array of revision identifier strings
476 477 478 479 480 |
# File 'lib/chill.rb', line 476 def revisions request = @database.http("#{URI.escape self['_id']}?revs=true").get json = JSON.parse(request.body) json['_revisions']['ids'] end |
#to_param ⇒ Object
to_param is supplied for integration with url routers in web frameworks like Rails and Camping. Defaults to returning the document’s _id. You can just pass documents in to functions which generate urls in your views in many ruby frameworks, and it will be understood as the _id of the document
If you have a more intelligent behaviour, monkeypatch in some special behaviour.
Returns:
(String): self['_id']
492 493 494 |
# File 'lib/chill.rb', line 492 def to_param self['_id'] end |