Class: Cushion::Document
- Inherits:
-
Object
- Object
- Cushion::Document
- Defined in:
- lib/cushion/document.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#database ⇒ Object
Returns the value of attribute database.
-
#headers ⇒ Object
Returns the value of attribute headers.
Class Method Summary collapse
- .database ⇒ Object
-
.set_database(db) ⇒ Object
Sets the default database for the document class.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#attach(filename, data, content_type = "application/octet-stream") ⇒ Object
Attach a file to this document.
- #attachments ⇒ Object
-
#copy_to(dest_id, opts = {}) ⇒ Object
Copies this document to a new id.
- #delete(key) ⇒ Object
-
#destroy ⇒ Object
Deletes this document.
-
#detach(filename) ⇒ Object
Deletes the attachment identified by
filename. -
#fetch(filename) ⇒ Object
Opens the attachment identified by
filename. - #id ⇒ Object
- #id? ⇒ Boolean (also: #has_id?)
-
#initialize(atts = {}) ⇒ Document
constructor
A new instance of Document.
- #key?(key) ⇒ Boolean (also: #has_key?, #attribute?)
- #merge!(atts) ⇒ Object
-
#move_to(dest_id, opts = {}) ⇒ Object
Moves this document to a new id.
-
#new_document? ⇒ Boolean
(also: #new_doc?)
Returns true if the document is unsaved.
-
#reload ⇒ Object
Reloads this document.
-
#rename(oldname, newname) ⇒ Object
Renames an attachment.
- #rev ⇒ Object (also: #etag)
- #rev? ⇒ Boolean (also: #has_rev?)
-
#save ⇒ Object
Save this document.
- #to_json ⇒ Object
Constructor Details
#initialize(atts = {}) ⇒ Document
Returns a new instance of Document.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cushion/document.rb', line 14 def initialize(atts = {}) @attributes = {} @headers = {} merge!(atts) if atts.respond_to?(:headers) atts.headers.each do |h,v| @headers[h] = v end end end |
Instance Attribute Details
#database ⇒ Object
Returns the value of attribute database.
3 4 5 |
# File 'lib/cushion/document.rb', line 3 def database @database end |
#headers ⇒ Object
Returns the value of attribute headers.
3 4 5 |
# File 'lib/cushion/document.rb', line 3 def headers @headers end |
Class Method Details
.database ⇒ Object
10 11 12 |
# File 'lib/cushion/document.rb', line 10 def self.database @database end |
.set_database(db) ⇒ Object
Sets the default database for the document class.
6 7 8 |
# File 'lib/cushion/document.rb', line 6 def self.set_database(db) @database = db end |
Instance Method Details
#[](key) ⇒ Object
35 36 37 |
# File 'lib/cushion/document.rb', line 35 def [](key) @attributes[key.to_s] end |
#[]=(key, value) ⇒ Object
25 26 27 |
# File 'lib/cushion/document.rb', line 25 def []=(key, value) @attributes[key.to_s] = value end |
#attach(filename, data, content_type = "application/octet-stream") ⇒ Object
Attach a file to this document. Will overwrite an existing file.
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/cushion/document.rb', line 98 def attach(filename, data, content_type = "application/octet-stream") result = database.attach(id, filename, data, :rev => rev, :content_type => content_type) if result['ok'] self[:_rev] = result['rev'] [filename] = { "stub" => true, "content_type" => content_type, "length" => "<not loaded>" } end result['ok'] end |
#attachments ⇒ Object
72 73 74 |
# File 'lib/cushion/document.rb', line 72 def self[:_attachments] ||= {} end |
#copy_to(dest_id, opts = {}) ⇒ Object
Copies this document to a new id. If dest_id currently exists then the dest_rev option must be provided.
147 148 149 150 |
# File 'lib/cushion/document.rb', line 147 def copy_to(dest_id, opts = {}) result = database.copy(id, dest_id, :dest_rev => opts[:rev]) result['ok'] end |
#delete(key) ⇒ Object
39 40 41 |
# File 'lib/cushion/document.rb', line 39 def delete(key) @attributes.delete(key.to_s) end |
#destroy ⇒ Object
Deletes this document.
88 89 90 91 92 93 94 95 |
# File 'lib/cushion/document.rb', line 88 def destroy result = database.destroy(id, :rev => rev) if result['ok'] self[:_rev] = nil self[:_id] = nil end result['ok'] end |
#detach(filename) ⇒ Object
Deletes the attachment identified by filename.
113 114 115 116 117 118 119 120 |
# File 'lib/cushion/document.rb', line 113 def detach(filename) result = database.destroy(id, filename, :rev => rev) if result['ok'] self[:_rev] = result['rev'] .delete(filename) end result['ok'] end |
#fetch(filename) ⇒ Object
Opens the attachment identified by filename. Returns either a String or a Tempfile, depending on the database setup.
124 125 126 |
# File 'lib/cushion/document.rb', line 124 def fetch(filename) database.fetch(id, filename) end |
#id ⇒ Object
53 54 55 |
# File 'lib/cushion/document.rb', line 53 def id self[:_id] end |
#id? ⇒ Boolean Also known as: has_id?
57 58 59 |
# File 'lib/cushion/document.rb', line 57 def id? (id && !id.empty?) ? true : false end |
#key?(key) ⇒ Boolean Also known as: has_key?, attribute?
43 44 45 |
# File 'lib/cushion/document.rb', line 43 def key?(key) @attributes.key?(key.to_s) end |
#merge!(atts) ⇒ Object
29 30 31 32 33 |
# File 'lib/cushion/document.rb', line 29 def merge!(atts) atts.each do |k,v| self[k.to_s] = v end end |
#move_to(dest_id, opts = {}) ⇒ Object
Moves this document to a new id. If dest_id currently exists then the dest_rev option must be provided.
154 155 156 157 158 159 160 161 |
# File 'lib/cushion/document.rb', line 154 def move_to(dest_id, opts = {}) result = database.move(id, dest_id, :rev => rev, :dest_rev => opts[:rev]) if result['ok'] self[:_rev] = nil self[:_id] = nil end result['ok'] end |
#new_document? ⇒ Boolean Also known as: new_doc?
Returns true if the document is unsaved.
77 78 79 |
# File 'lib/cushion/document.rb', line 77 def new_document? !rev? end |
#reload ⇒ Object
Reloads this document.
140 141 142 143 |
# File 'lib/cushion/document.rb', line 140 def reload initialize(database.fetch(id)) self end |
#rename(oldname, newname) ⇒ Object
Renames an attachment. oldname is the name of the existing attachment and newname is the desired name.
130 131 132 133 134 135 136 137 |
# File 'lib/cushion/document.rb', line 130 def rename(oldname, newname) result = database.(id, oldname, newname) if result['ok'] self[:_rev] = result['rev'] [newname] = .delete(oldname) end result['ok'] end |
#rev ⇒ Object Also known as: etag
62 63 64 |
# File 'lib/cushion/document.rb', line 62 def rev self[:_rev] end |
#rev? ⇒ Boolean Also known as: has_rev?
67 68 69 |
# File 'lib/cushion/document.rb', line 67 def rev? (rev && !rev.empty?) ? true : false end |
#save ⇒ Object
Save this document. TODO: Change attachments to stubs on save?
83 84 85 |
# File 'lib/cushion/document.rb', line 83 def save database.store(self)["ok"] end |
#to_json ⇒ Object
163 164 165 |
# File 'lib/cushion/document.rb', line 163 def to_json @attributes.to_json end |