Module: CouchCrumbs::Document::InstanceMethods
- Includes:
- Query
- Defined in:
- lib/couch_crumbs/document.rb
Instance Method Summary collapse
-
#after_create ⇒ Object
Hook called during #create! after a document has #saved!.
-
#after_destroy ⇒ Object
Hook called during #destroy! after a document has been destroyed.
-
#after_initialize ⇒ Object
Hook called after a document has been initialized.
-
#after_save ⇒ Object
Hook called during #save! after a document has #saved!.
-
#before_create ⇒ Object
Hook called during #create! before a document is #saved!.
-
#before_destroy ⇒ Object
Hook called during #destroy! before a document has been destroyed.
-
#before_save ⇒ Object
Hook called during #save! before a document has #saved!.
-
#crumb_type ⇒ Object
Return the CouchCrumb document type.
-
#database ⇒ Object
Return the class-based database.
-
#destroy! ⇒ Object
Remove document from the database.
-
#id ⇒ Object
Return document id (typically a UUID).
-
#id=(new_id) ⇒ Object
Set the document id.
-
#new_document? ⇒ Boolean
Return true prior to document being saved.
-
#rev ⇒ Object
Return document revision.
-
#save! ⇒ Object
Save a document to a database.
-
#update_attributes!(attributes = {}) ⇒ Object
Update and save the named properties.
Methods included from Query
Instance Method Details
#after_create ⇒ Object
Hook called during #create! after a document has #saved!
121 122 123 |
# File 'lib/couch_crumbs/document.rb', line 121 def after_create nil end |
#after_destroy ⇒ Object
Hook called during #destroy! after a document has been destroyed
145 146 147 |
# File 'lib/couch_crumbs/document.rb', line 145 def after_destroy nil end |
#after_initialize ⇒ Object
Hook called after a document has been initialized
109 110 111 |
# File 'lib/couch_crumbs/document.rb', line 109 def after_initialize nil end |
#after_save ⇒ Object
Hook called during #save! after a document has #saved!
133 134 135 |
# File 'lib/couch_crumbs/document.rb', line 133 def after_save nil end |
#before_create ⇒ Object
Hook called during #create! before a document is #saved!
115 116 117 |
# File 'lib/couch_crumbs/document.rb', line 115 def before_create nil end |
#before_destroy ⇒ Object
Hook called during #destroy! before a document has been destroyed
139 140 141 |
# File 'lib/couch_crumbs/document.rb', line 139 def before_destroy nil end |
#before_save ⇒ Object
Hook called during #save! before a document has #saved!
127 128 129 |
# File 'lib/couch_crumbs/document.rb', line 127 def before_save nil end |
#crumb_type ⇒ Object
Return the CouchCrumb document type
38 39 40 |
# File 'lib/couch_crumbs/document.rb', line 38 def crumb_type raw["crumb_type"] end |
#database ⇒ Object
Return the class-based database
13 14 15 |
# File 'lib/couch_crumbs/document.rb', line 13 def database self.class.database end |
#destroy! ⇒ Object
Remove document from the database
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/couch_crumbs/document.rb', line 84 def destroy! before_destroy freeze # destruction status status = nil # Since new documents haven't been saved yet, and frozen documents # *can't* be saved, simply return true here. if new_document? status = true else result = JSON.parse(RestClient.delete(File.join(uri, "?rev=#{ rev }"))) status = result["ok"] end after_destroy status end |
#id ⇒ Object
Return document id (typically a UUID)
19 20 21 |
# File 'lib/couch_crumbs/document.rb', line 19 def id raw["_id"] end |
#id=(new_id) ⇒ Object
Set the document id
24 25 26 27 28 |
# File 'lib/couch_crumbs/document.rb', line 24 def id=(new_id) raise "only new documents may set an id" unless new_document? raw["_id"] = new_id end |
#new_document? ⇒ Boolean
Return true prior to document being saved
78 79 80 |
# File 'lib/couch_crumbs/document.rb', line 78 def new_document? raw["_rev"].eql?(nil) end |
#rev ⇒ Object
Return document revision
32 33 34 |
# File 'lib/couch_crumbs/document.rb', line 32 def rev raw["_rev"] end |
#save! ⇒ Object
Save a document to a database
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/couch_crumbs/document.rb', line 44 def save! raise "unable to save frozen documents" if frozen? # Before Callback before_save # Update timestamps raw["updated_at"] = Time.now if self.class.properties.include?(:updated_at) # Save to the DB result = JSON.parse(RestClient.put(uri, raw.to_json)) # Update ID and Rev properties raw["_id"] = result["id"] raw["_rev"] = result["rev"] # After callback after_save result["ok"] end |
#update_attributes!(attributes = {}) ⇒ Object
Update and save the named properties
68 69 70 71 72 73 74 |
# File 'lib/couch_crumbs/document.rb', line 68 def update_attributes!(attributes = {}) attributes.each_pair do |key, value| raw[key.to_s] = value end save! end |