Class: CouchDB::Document
- Inherits:
-
Object
show all
- Defined in:
- lib/couchdb/document.rb
Overview
Base is the main super class of all models that should be stored in CouchDB. See the README file for more information.
Direct Known Subclasses
Design
Defined Under Namespace
Classes: NotFoundError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(database, properties = { }) ⇒ Document
Returns a new instance of Document.
14
15
16
17
|
# File 'lib/couchdb/document.rb', line 14
def initialize(database, properties = { })
@database = database
@properties = properties
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
116
117
118
|
# File 'lib/couchdb/document.rb', line 116
def method_missing(method_name, *arguments, &block)
@properties.send method_name, *arguments, &block
end
|
Instance Attribute Details
#database ⇒ Object
Returns the value of attribute database.
12
13
14
|
# File 'lib/couchdb/document.rb', line 12
def database
@database
end
|
Instance Method Details
#==(other) ⇒ Object
51
52
53
|
# File 'lib/couchdb/document.rb', line 51
def ==(other)
self.id == other.id
end
|
#[](key) ⇒ Object
19
20
21
|
# File 'lib/couchdb/document.rb', line 19
def [](key)
@properties[key.to_s]
end
|
#[]=(key, value) ⇒ Object
23
24
25
|
# File 'lib/couchdb/document.rb', line 23
def []=(key, value)
@properties[key.to_s] = value
end
|
#clear_rev ⇒ Object
47
48
49
|
# File 'lib/couchdb/document.rb', line 47
def clear_rev
@properties.delete "_rev"
end
|
#destroy ⇒ Object
79
80
81
82
83
84
85
86
|
# File 'lib/couchdb/document.rb', line 79
def destroy
return false if new?
Transport::JSON.request :delete, url, :headers => { "If-Match" => self.rev }, :expected_status_code => 200
self.clear_rev
true
rescue Transport::UnexpectedStatusCodeError => error
upgrade_unexpected_status_error error
end
|
#exists? ⇒ Boolean
59
60
61
62
63
64
65
|
# File 'lib/couchdb/document.rb', line 59
def exists?
Transport::JSON.request :get, url, :expected_status_code => 200
true
rescue Transport::UnexpectedStatusCodeError => error
raise error unless error.status_code == 404
false
end
|
#id ⇒ Object
27
28
29
|
# File 'lib/couchdb/document.rb', line 27
def id
self["_id"]
end
|
#id=(value) ⇒ Object
31
32
33
|
# File 'lib/couchdb/document.rb', line 31
def id=(value)
self["_id"] = value
end
|
#load ⇒ Object
Also known as:
reload
67
68
69
70
71
72
|
# File 'lib/couchdb/document.rb', line 67
def load
@properties = Transport::JSON.request :get, url, :expected_status_code => 200
true
rescue Transport::UnexpectedStatusCodeError => error
upgrade_unexpected_status_error error
end
|
#new? ⇒ Boolean
55
56
57
|
# File 'lib/couchdb/document.rb', line 55
def new?
!self.rev?
end
|
#rev ⇒ Object
35
36
37
|
# File 'lib/couchdb/document.rb', line 35
def rev
self["_rev"]
end
|
#rev=(value) ⇒ Object
39
40
41
|
# File 'lib/couchdb/document.rb', line 39
def rev=(value)
self["_rev"] = value
end
|
#rev? ⇒ Boolean
43
44
45
|
# File 'lib/couchdb/document.rb', line 43
def rev?
@properties.has_key? "_rev"
end
|
#save ⇒ Object
75
76
77
|
# File 'lib/couchdb/document.rb', line 75
def save
new? ? create : update
end
|
#url ⇒ Object
88
89
90
|
# File 'lib/couchdb/document.rb', line 88
def url
"#{self.database.url}/#{self.id}"
end
|