Class: Parse::Object
- Inherits:
-
Object
show all
- Defined in:
- lib/parse/object.rb
Constant Summary
collapse
- @@parse_class_vs_class_table =
{}
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash = {}) ⇒ Object
Returns a new instance of Object.
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/parse/object.rb', line 71
def initialize hash={}
body_hash = nil
hash = string_keyed_hash hash
if hash.has_key? 'objectId'
@parse_object_id = hash['objectId']
@raw_hash = hash
@updated_hash = {}
body_hash = @raw_hash
else
@raw_hash = {}
@updated_hash = hash
body_hash = @updated_hash
end
@deleted = false
hash.each do |k, v|
if v.is_a? Hash
body_hash[k] =
case v['__type']
when 'Date'
Date.parse v['iso']
when 'File'
Parse::File.new v
when 'Pointer'
Parse::Pointer.new v, self
when 'Relation'
Parse::Relation.new self, k, v
else
v
end
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
227
228
229
230
231
232
233
234
235
|
# File 'lib/parse/object.rb', line 227
def method_missing name, *args, &block
if name =~ /^\w+$/ && args.empty? && block.nil?
get_column name
elsif name[-1] == '=' && args.size == 1 && block.nil?
set_column name[0..-2], args.first
else
super
end
end
|
Class Attribute Details
.auto_camel_case ⇒ Object
Returns the value of attribute auto_camel_case.
13
14
15
|
# File 'lib/parse/object.rb', line 13
def auto_camel_case
@auto_camel_case
end
|
.parse_class_name ⇒ Object
Returns the value of attribute parse_class_name.
13
14
15
|
# File 'lib/parse/object.rb', line 13
def parse_class_name
@parse_class_name
end
|
.parse_client ⇒ Object
Returns the value of attribute parse_client.
13
14
15
|
# File 'lib/parse/object.rb', line 13
def parse_client
@parse_client
end
|
Instance Attribute Details
Returns the value of attribute acl.
69
70
71
|
# File 'lib/parse/object.rb', line 69
def acl
@acl
end
|
#created_at ⇒ Object
Returns the value of attribute created_at.
69
70
71
|
# File 'lib/parse/object.rb', line 69
def created_at
@created_at
end
|
#parse_object_id ⇒ Object
Also known as:
obj_id
Returns the value of attribute parse_object_id.
69
70
71
|
# File 'lib/parse/object.rb', line 69
def parse_object_id
@parse_object_id
end
|
#updated_at ⇒ Object
Returns the value of attribute updated_at.
69
70
71
|
# File 'lib/parse/object.rb', line 69
def updated_at
@updated_at
end
|
Class Method Details
.create(parse_class_name, mod = ::Object) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/parse/object.rb', line 19
def create parse_class_name, mod=::Object
raise 'already defined' if mod.const_defined? parse_class_name
if RESERVED_PARSE_CLASS.has_key? parse_class_name.to_s
eval RESERVED_PARSE_CLASS[parse_class_name.to_s]
else
klass = Class.new(Parse::Object)
klass.parse_class_name = parse_class_name.to_sym
klass.auto_camel_case = true
mod.const_set parse_class_name, klass
register_parse_class klass
end
end
|
.find(object_id_or_conditions, opts = {}) ⇒ Object
41
42
43
44
|
# File 'lib/parse/object.rb', line 41
def find object_id_or_conditions, opts={}
results = [parse_client.find(self.parse_class_name, object_id_or_conditions, opts)].flatten
results.map! {|hash| self.new hash}
end
|
.find!(object_id_or_conditions, opts = {}) ⇒ Object
51
52
53
54
|
# File 'lib/parse/object.rb', line 51
def find! object_id_or_conditions, opts={}
results = [parse_client.find!(self.parse_class_name, object_id_or_conditions, opts)].flatten
results.map! {|hash| self.new hash}
end
|
.find_all(opts = {}) ⇒ Object
60
61
62
|
# File 'lib/parse/object.rb', line 60
def find_all opts={}
find :all, opts
end
|
.find_all!(opts = {}) ⇒ Object
64
65
66
|
# File 'lib/parse/object.rb', line 64
def find_all! opts={}
find! :all, opts
end
|
.find_by_id(object_id, opts = {}) ⇒ Object
46
47
48
|
# File 'lib/parse/object.rb', line 46
def find_by_id object_id, opts={}
find(object_id, opts).first
end
|
.find_by_id!(object_id, opts = {}) ⇒ Object
56
57
58
|
# File 'lib/parse/object.rb', line 56
def find_by_id! object_id, opts={}
find!(object_id, opts).first
end
|
.register_parse_class(parse_class) ⇒ Object
15
16
17
|
# File 'lib/parse/object.rb', line 15
def register_parse_class parse_class
@@parse_class_vs_class_table[parse_class.parse_class_name] = parse_class
end
|
Instance Method Details
#create(hash, use_master_key = false) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/parse/object.rb', line 144
def create hash, use_master_key=false
check_deleted!
hash = string_keyed_hash hash
@updated_hash.update hash
method = use_master_key ? :create! : :create
parse_client.send(method, self.parse_class_name, @updated_hash) do |response|
@parse_object_id = response['objectId']
@created_at = Date.parse response['createdAt']
@updated_at = @created_at
@raw_hash.update @updated_hash
@raw_hash.update response
@updated_hash.clear
end
end
|
#create!(hash) ⇒ Object
159
160
161
|
# File 'lib/parse/object.rb', line 159
def create! hash
create hash, true
end
|
#delete(use_master_key = false) ⇒ Object
179
180
181
182
183
184
185
186
|
# File 'lib/parse/object.rb', line 179
def delete use_master_key=false
raise 'You cannot delete new object' if new?
check_deleted!
method = use_master_key ? :delete! : :delete
parse_client.send(method, parse_class_name, parse_object_id) do |response|
@deleted = true
end
end
|
188
189
190
|
# File 'lib/parse/object.rb', line 188
def delete!
delete true
end
|
#deleted? ⇒ Boolean
113
114
115
|
# File 'lib/parse/object.rb', line 113
def deleted?
@deleted
end
|
#get_column(name) ⇒ Object
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/parse/object.rb', line 199
def get_column name
name = name.to_s
ret = @updated_hash[name]
if ret.nil? && self.class.auto_camel_case
ret = @updated_hash[name.camelize :lower]
end
if ret.nil?
ret = @raw_hash[name]
if ret.nil? && self.class.auto_camel_case
ret = @raw_hash[name.camelize :lower]
end
end
ret
end
|
#new? ⇒ Boolean
105
106
107
|
# File 'lib/parse/object.rb', line 105
def new?
!@deleted && @raw_hash.empty?
end
|
#parse_class_name ⇒ Object
121
122
123
|
# File 'lib/parse/object.rb', line 121
def parse_class_name
self.class.parse_class_name
end
|
#parse_client ⇒ Object
117
118
119
|
# File 'lib/parse/object.rb', line 117
def parse_client
self.class.parse_client
end
|
219
220
221
|
# File 'lib/parse/object.rb', line 219
def pointer
Pointer.new 'className' => parse_class_name, 'objectId' => parse_object_id
end
|
#save(hash = @updated_hash, use_master_key = false) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/parse/object.rb', line 125
def save hash=@updated_hash, use_master_key=false
check_deleted!
unless hash.is_a? Hash
hash = @updated_hash
use_master_key = hash
end
hash = string_keyed_hash hash
if new?
create hash, use_master_key
else
update hash, use_master_key
end
end
|
#save!(hash = @updated_hash) ⇒ Object
140
141
142
|
# File 'lib/parse/object.rb', line 140
def save! hash=@updated_hash
save hash, true
end
|
#set_column(name, value) ⇒ Object
214
215
216
217
|
# File 'lib/parse/object.rb', line 214
def set_column name, value
check_deleted!
@updated_hash[name] = value
end
|
223
224
225
|
# File 'lib/parse/object.rb', line 223
def to_s
"<#{parse_class_name}: #{{}.update(@raw_hash).update(@updated_hash).to_s}>"
end
|
#update(hash, use_master_key = false) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/parse/object.rb', line 163
def update hash, use_master_key=false
check_deleted!
hash = string_keyed_hash hash
method = use_master_key ? :update! : :update
parse_client.send(method, parse_class_name, parse_object_id, hash) do |response|
@raw_hash.update @updated_hash
@raw_hash.update response
@updated_at = Date.parse response['updatedAt']
@updated_hash.clear
end
end
|
#update!(hash) ⇒ Object
175
176
177
|
# File 'lib/parse/object.rb', line 175
def update! hash
update hash, true
end
|
#updated? ⇒ Boolean
109
110
111
|
# File 'lib/parse/object.rb', line 109
def updated?
!@deleted && !@updated_hash.empty?
end
|