Class: Couch::Document
Direct Known Subclasses
View
Constant Summary
Constants included
from Log
Log::QUIET
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
condition_keys, count, view
Constructor Details
#initialize(data = {}, dont_change = true) ⇒ Document
Returns a new instance of Document.
42
43
44
45
46
47
|
# File 'lib/couch/document.rb', line 42
def initialize(data = {}, dont_change = true)
@attributes = data.dup
self.id = @attributes.delete(:id) if @attributes[:id]
@attributes[:type] = self.class.name
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/couch/document.rb', line 129
def method_missing(sym, *args)
if block_given?
super
elsif args.length == 0 && attributes.key?(sym)
attributes[sym]
elsif args.length == 1 && sym.to_s =~ /(.+)=$/
attributes[$1.to_sym] = args.first
else
super
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
40
41
42
|
# File 'lib/couch/document.rb', line 40
def attributes
@attributes
end
|
Class Method Details
.async(&block) ⇒ Object
12
13
14
|
# File 'lib/couch/document.rb', line 12
def self.async(&block)
database.async(&block)
end
|
.create(*args) ⇒ Object
16
17
18
|
# File 'lib/couch/document.rb', line 16
def self.create(*args)
new(*args).save
end
|
4
5
6
|
# File 'lib/couch/document.rb', line 4
def self.database
Couch.database
end
|
34
35
36
37
38
|
# File 'lib/couch/document.rb', line 34
def self.find(id)
database.read(id)
rescue Couch::Error404
nil
end
|
.instantiate(data, target = nil) ⇒ Object
instantiate a Couch::Document from a Hash.
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/couch/document.rb', line 21
def self.instantiate(data, target = nil)
target_type_name = data[:type] || raise("Missing document type in #{data.inspect}")
if target.nil?
target_klass = target_type_name.constantize
target = target_klass.new(data)
elsif target.class.name != target_type_name
raise "Type mismatch #{target.class.name} vs #{target_type_name}"
target.update data
end
target
end
|
Instance Method Details
#changed? ⇒ Boolean
98
99
100
|
# File 'lib/couch/document.rb', line 98
def changed?
@original != @attributes
end
|
8
9
10
|
# File 'lib/couch/document.rb', line 8
def database
self.class.database
end
|
102
103
104
105
|
# File 'lib/couch/document.rb', line 102
def destroy
database.destroy self
self
end
|
49
50
51
52
53
54
|
# File 'lib/couch/document.rb', line 49
def dup
other = super
other.instance_variable_set "@attributes", attributes.dup
other.instance_variable_set "@original", @original.dup
other
end
|
58
|
# File 'lib/couch/document.rb', line 58
def id; @attributes[:_id]; end
|
59
|
# File 'lib/couch/document.rb', line 59
def id=(id); @attributes[:_id] = id; end
|
107
108
109
|
# File 'lib/couch/document.rb', line 107
def inspect
"<#{self.class.name}[#{self.id}] #{attributes.inspect}>"
end
|
#new_record? ⇒ Boolean
75
76
77
|
# File 'lib/couch/document.rb', line 75
def new_record?
rev.nil?
end
|
79
80
81
82
83
84
85
|
# File 'lib/couch/document.rb', line 79
def reload
raise if new_record?
@attributes = database.read(self.id).attributes
@original = attributes.dup
self
end
|
61
|
# File 'lib/couch/document.rb', line 61
def rev; @attributes[:_rev]; end
|
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/couch/document.rb', line 87
def save
if new_record?
database.create self
elsif changed?
database.update self
end
@original = attributes.dup
self
end
|
63
64
65
66
67
68
69
70
|
# File 'lib/couch/document.rb', line 63
def to_hash
r = @attributes.dup
r.delete :_id
r.delete :_rev
r.delete :type
r
end
|
119
120
121
|
# File 'lib/couch/document.rb', line 119
def to_json
attributes.to_json
end
|
56
|
# File 'lib/couch/document.rb', line 56
def type; attributes[:type]; end
|
#update(attributes) ⇒ Object
123
124
125
|
# File 'lib/couch/document.rb', line 123
def update(attributes)
self.attributes.update attributes
end
|