parse_resource 1.5.11 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +36 -7
- data/VERSION +1 -1
- data/lib/base.rb +289 -0
- data/lib/examples/post.rb +4 -8
- data/lib/parse_resource.rb +2 -242
- data/lib/parse_resource_old.rb +286 -0
- data/parse_resource.gemspec +4 -2
- data/rdoc/ParseResource.html +253 -261
- data/rdoc/created.rid +4 -3
- data/rdoc/index.html +5 -7
- data/rdoc/lib/parse_resource_rb.html +16 -2
- data/test/test_parse_resource.rb +2 -2
- metadata +25 -23
@@ -0,0 +1,286 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
require "active_model"
|
4
|
+
require "erb"
|
5
|
+
require "rest-client"
|
6
|
+
require "json"
|
7
|
+
require "active_support/hash_with_indifferent_access"
|
8
|
+
|
9
|
+
class ParseResource
|
10
|
+
# ParseResource provides an easy way to use Ruby to interace with a Parse.com backend
|
11
|
+
# Usage:
|
12
|
+
# class Post < ParseResource
|
13
|
+
# fields :title, :author, :body
|
14
|
+
# end
|
15
|
+
|
16
|
+
include ActiveModel::Validations
|
17
|
+
include ActiveModel::Conversion
|
18
|
+
include ActiveModel::AttributeMethods
|
19
|
+
extend ActiveModel::Naming
|
20
|
+
extend ActiveModel::Callbacks
|
21
|
+
HashWithIndifferentAccess = ActiveSupport::HashWithIndifferentAccess
|
22
|
+
|
23
|
+
# define_model_callbacks :initialize, :find, :only => :after
|
24
|
+
define_model_callbacks :save, :create, :update, :destroy
|
25
|
+
|
26
|
+
|
27
|
+
# Instantiates a ParseResource object
|
28
|
+
#
|
29
|
+
# @params [Hash], [Boolean] a `Hash` of attributes and a `Boolean` that should be false only if the object already exists
|
30
|
+
# @return [ParseResource] an object that subclasses `Parseresource`
|
31
|
+
def initialize(attributes = {}, new=true)
|
32
|
+
attributes = HashWithIndifferentAccess.new(attributes)
|
33
|
+
if new
|
34
|
+
@unsaved_attributes = attributes
|
35
|
+
else
|
36
|
+
@unsaved_attributes = {}
|
37
|
+
end
|
38
|
+
self.attributes = {}
|
39
|
+
self.attributes.merge!(attributes)
|
40
|
+
self.attributes unless self.attributes.empty?
|
41
|
+
create_setters!
|
42
|
+
end
|
43
|
+
|
44
|
+
# Explicitly adds a field to the model.
|
45
|
+
#
|
46
|
+
# @param [Symbol] name the name of the field, eg `:author`.
|
47
|
+
# @param [Boolean] val the return value of the field. Only use this within the class.
|
48
|
+
def self.field(name, val=nil)
|
49
|
+
class_eval do
|
50
|
+
define_method(name) do
|
51
|
+
@attributes[name] ? @attributes[name] : @unsaved_attributes[name]
|
52
|
+
end
|
53
|
+
define_method("#{name}=") do |val|
|
54
|
+
@attributes[name] = val
|
55
|
+
@unsaved_attributes[name] = val
|
56
|
+
val
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Add multiple fields in one line. Same as `#field`, but accepts multiple args.
|
62
|
+
#
|
63
|
+
# @param [Array] *args an array of `Symbol`s, `eg :author, :body, :title`.
|
64
|
+
def self.fields(*args)
|
65
|
+
args.each {|f| field(f)}
|
66
|
+
end
|
67
|
+
|
68
|
+
# Creates getter and setter methods for model fields
|
69
|
+
#
|
70
|
+
def create_setters!
|
71
|
+
@attributes.each_pair do |k,v|
|
72
|
+
self.class.send(:define_method, "#{k}=") do |val|
|
73
|
+
if k.is_a?(Symbol)
|
74
|
+
k = k.to_s
|
75
|
+
end
|
76
|
+
@attributes[k.to_s] = val
|
77
|
+
@unsaved_attributes[k.to_s] = val
|
78
|
+
val
|
79
|
+
end
|
80
|
+
self.class.send(:define_method, "#{k}") do
|
81
|
+
if k.is_a?(Symbol)
|
82
|
+
k = k.to_s
|
83
|
+
end
|
84
|
+
|
85
|
+
@attributes[k.to_s]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class << self
|
91
|
+
def has_one(child_name)
|
92
|
+
class_eval do
|
93
|
+
|
94
|
+
define_method("#{child_name}") do
|
95
|
+
child_name
|
96
|
+
end
|
97
|
+
|
98
|
+
define_method("#{child_name}=") do |child_object|
|
99
|
+
[child_object, child_name]
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def belongs_to(name)
|
106
|
+
class_eval do
|
107
|
+
|
108
|
+
define_method("#{parent_name}") do
|
109
|
+
name
|
110
|
+
end
|
111
|
+
|
112
|
+
define_method("#{parent_name}=") do |parent_object|
|
113
|
+
[parent_name, parent_object]
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
@@settings ||= nil
|
121
|
+
|
122
|
+
# Explicitly set Parse.com API keys.
|
123
|
+
#
|
124
|
+
# @param [String] app_id the Application ID of your Parse database
|
125
|
+
# @param [String] master_key the Master Key of your Parse database
|
126
|
+
def load!(app_id, master_key)
|
127
|
+
@@settings = {"app_id" => app_id, "master_key" => master_key}
|
128
|
+
end
|
129
|
+
|
130
|
+
# Creates a RESTful resource
|
131
|
+
# sends requests to [base_uri]/[classname]
|
132
|
+
#
|
133
|
+
def resource
|
134
|
+
if @@settings.nil?
|
135
|
+
path = "config/parse_resource.yml"
|
136
|
+
environment = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : ENV["RACK_ENV"]
|
137
|
+
@@settings = YAML.load(ERB.new(File.new(path).read).result)[environment]
|
138
|
+
end
|
139
|
+
base_uri = "https://api.parse.com/1/classes/#{model_name}"
|
140
|
+
app_id = @@settings['app_id']
|
141
|
+
master_key = @@settings['master_key']
|
142
|
+
RestClient::Resource.new(base_uri, app_id, master_key)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Find a ParseResource object by ID
|
146
|
+
#
|
147
|
+
# @param [String] id the ID of the Parse object you want to find.
|
148
|
+
# @return [ParseResource] an object that subclasses ParseResource.
|
149
|
+
def find(id)
|
150
|
+
where(:objectId => id).first
|
151
|
+
end
|
152
|
+
|
153
|
+
# Find a ParseResource object by a `Hash` of conditions.
|
154
|
+
#
|
155
|
+
# @param [Hash] parameters a `Hash` of conditions.
|
156
|
+
# @return [Array] an `Array` of objects that subclass `ParseResource`.
|
157
|
+
def where(parameters)
|
158
|
+
resp = resource.get(:params => {:where => parameters.to_json})
|
159
|
+
results = JSON.parse(resp)['results']
|
160
|
+
results.map {|r| model_name.constantize.new(r, false)}
|
161
|
+
end
|
162
|
+
|
163
|
+
# Find all ParseResource objects for that model.
|
164
|
+
#
|
165
|
+
# @return [Array] an `Array` of objects that subclass `ParseResource`.
|
166
|
+
def all
|
167
|
+
resp = resource.get
|
168
|
+
results = JSON.parse(resp)['results']
|
169
|
+
results.map {|r| model_name.constantize.new(r, false)}
|
170
|
+
end
|
171
|
+
|
172
|
+
# Create a ParseResource object.
|
173
|
+
#
|
174
|
+
# @param [Hash] attributes a `Hash` of attributes
|
175
|
+
# @return [ParseResource] an object that subclasses `ParseResource`. Or returns `false` if object fails to save.
|
176
|
+
def create(attributes = {})
|
177
|
+
attributes = HashWithIndifferentAccess.new(attributes)
|
178
|
+
new(attributes).save
|
179
|
+
end
|
180
|
+
|
181
|
+
# Find the first object. Fairly random, not based on any specific condition.
|
182
|
+
#
|
183
|
+
def first
|
184
|
+
all.first
|
185
|
+
end
|
186
|
+
|
187
|
+
def class_attributes
|
188
|
+
@class_attributes ||= {}
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
def persisted?
|
194
|
+
if id
|
195
|
+
true
|
196
|
+
else
|
197
|
+
false
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def new?
|
202
|
+
!persisted?
|
203
|
+
end
|
204
|
+
|
205
|
+
# delegate from Class method
|
206
|
+
def resource
|
207
|
+
self.class.resource
|
208
|
+
end
|
209
|
+
|
210
|
+
# create RESTful resource for the specific Parse object
|
211
|
+
# sends requests to [base_uri]/[classname]/[objectId]
|
212
|
+
def instance_resource
|
213
|
+
self.class.resource["#{self.id}"]
|
214
|
+
end
|
215
|
+
|
216
|
+
def create
|
217
|
+
resp = self.resource.post(@unsaved_attributes.to_json, :content_type => "application/json")
|
218
|
+
@attributes.merge!(JSON.parse(resp))
|
219
|
+
@attributes.merge!(@unsaved_attributes)
|
220
|
+
attributes = HashWithIndifferentAccess.new(attributes)
|
221
|
+
@unsaved_attributes = {}
|
222
|
+
create_setters!
|
223
|
+
self
|
224
|
+
end
|
225
|
+
|
226
|
+
def save
|
227
|
+
if valid?
|
228
|
+
run_callbacks :save do
|
229
|
+
new? ? create : update
|
230
|
+
end
|
231
|
+
else
|
232
|
+
false
|
233
|
+
end
|
234
|
+
rescue false
|
235
|
+
end
|
236
|
+
|
237
|
+
def update(attributes = {})
|
238
|
+
attributes = HashWithIndifferentAccess.new(attributes)
|
239
|
+
@unsaved_attributes.merge!(attributes)
|
240
|
+
|
241
|
+
put_attrs = @unsaved_attributes
|
242
|
+
put_attrs.delete('objectId')
|
243
|
+
put_attrs.delete('createdAt')
|
244
|
+
put_attrs.delete('updatedAt')
|
245
|
+
put_attrs = put_attrs.to_json
|
246
|
+
|
247
|
+
resp = self.instance_resource.put(put_attrs, :content_type => "application/json")
|
248
|
+
|
249
|
+
@attributes.merge!(JSON.parse(resp))
|
250
|
+
@attributes.merge!(@unsaved_attributes)
|
251
|
+
@unsaved_attributes = {}
|
252
|
+
create_setters!
|
253
|
+
|
254
|
+
self
|
255
|
+
end
|
256
|
+
|
257
|
+
def update_attributes(attributes = {})
|
258
|
+
self.update(attributes)
|
259
|
+
end
|
260
|
+
|
261
|
+
def destroy
|
262
|
+
self.instance_resource.delete
|
263
|
+
@attributes = {}
|
264
|
+
@unsaved_attributes = {}
|
265
|
+
nil
|
266
|
+
end
|
267
|
+
|
268
|
+
# provides access to @attributes for getting and setting
|
269
|
+
def attributes
|
270
|
+
@attributes ||= self.class.class_attributes
|
271
|
+
@attributes
|
272
|
+
end
|
273
|
+
|
274
|
+
def attributes=(n)
|
275
|
+
@attributes = n
|
276
|
+
@attributes
|
277
|
+
end
|
278
|
+
|
279
|
+
# aliasing for idiomatic Ruby
|
280
|
+
def id; self.objectId rescue nil; end
|
281
|
+
|
282
|
+
def created_at; self.createdAt; end
|
283
|
+
|
284
|
+
def updated_at; self.updatedAt rescue nil; end
|
285
|
+
|
286
|
+
end
|
data/parse_resource.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "parse_resource"
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.6.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alan deLevie"]
|
12
|
-
s.date = "2011-
|
12
|
+
s.date = "2011-12-07"
|
13
13
|
s.description = ""
|
14
14
|
s.email = "adelevie@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -24,8 +24,10 @@ Gem::Specification.new do |s|
|
|
24
24
|
"README.md",
|
25
25
|
"Rakefile",
|
26
26
|
"VERSION",
|
27
|
+
"lib/base.rb",
|
27
28
|
"lib/examples/post.rb",
|
28
29
|
"lib/parse_resource.rb",
|
30
|
+
"lib/parse_resource_old.rb",
|
29
31
|
"parse_resource.gemspec",
|
30
32
|
"rdoc/ParseResource.html",
|
31
33
|
"rdoc/created.rid",
|
data/rdoc/ParseResource.html
CHANGED
@@ -69,8 +69,6 @@
|
|
69
69
|
<h3 class="section-header">Methods</h3>
|
70
70
|
<ul class="link-list">
|
71
71
|
|
72
|
-
<li><a href="#method-c-add_field">::add_field</a></li>
|
73
|
-
|
74
72
|
<li><a href="#method-c-all">::all</a></li>
|
75
73
|
|
76
74
|
<li><a href="#method-c-class_attributes">::class_attributes</a></li>
|
@@ -109,8 +107,6 @@
|
|
109
107
|
|
110
108
|
<li><a href="#method-i-instance_resource">#instance_resource</a></li>
|
111
109
|
|
112
|
-
<li><a href="#method-i-method_missing">#method_missing</a></li>
|
113
|
-
|
114
110
|
<li><a href="#method-i-new%3F">#new?</a></li>
|
115
111
|
|
116
112
|
<li><a href="#method-i-persisted%3F">#persisted?</a></li>
|
@@ -173,6 +169,8 @@
|
|
173
169
|
|
174
170
|
<li><a href="./ParseResource.html">ParseResource</a></li>
|
175
171
|
|
172
|
+
<li><a href="./Post.html">Post</a></li>
|
173
|
+
|
176
174
|
</ul>
|
177
175
|
<div id="no-class-search-results" style="display: none;">No matching classes.</div>
|
178
176
|
</div>
|
@@ -190,6 +188,18 @@
|
|
190
188
|
|
191
189
|
<!-- Constants -->
|
192
190
|
|
191
|
+
<div id="constants-list" class="section">
|
192
|
+
<h3 class="section-header">Constants</h3>
|
193
|
+
<dl>
|
194
|
+
|
195
|
+
<dt><a name="HashWithIndifferentAccess">HashWithIndifferentAccess</a></dt>
|
196
|
+
|
197
|
+
<dd class="description"></dd>
|
198
|
+
|
199
|
+
|
200
|
+
</dl>
|
201
|
+
</div>
|
202
|
+
|
193
203
|
|
194
204
|
<!-- Attributes -->
|
195
205
|
|
@@ -200,40 +210,6 @@
|
|
200
210
|
<h3 class="section-header">Public Class Methods</h3>
|
201
211
|
|
202
212
|
|
203
|
-
<div id="add-field-method" class="method-detail ">
|
204
|
-
<a name="method-c-add_field"></a>
|
205
|
-
|
206
|
-
<div class="method-heading">
|
207
|
-
|
208
|
-
<span class="method-name">add_field</span><span
|
209
|
-
class="method-args">(fieldname, val=nil)</span>
|
210
|
-
<span class="method-click-advice">click to toggle source</span>
|
211
|
-
|
212
|
-
</div>
|
213
|
-
|
214
|
-
<div class="method-description">
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
<div class="method-source-code"
|
221
|
-
id="add-field-source">
|
222
|
-
<pre>
|
223
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 45</span>
|
224
|
-
45: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">add_field</span>(<span class="ruby-identifier">fieldname</span>, <span class="ruby-identifier">val</span>=<span class="ruby-keyword kw">nil</span>)
|
225
|
-
46: <span class="ruby-identifier">class_attributes</span>.<span class="ruby-identifier">merge!</span>({<span class="ruby-identifier">fieldname</span>.<span class="ruby-identifier">to_sym</span> =<span class="ruby-operator">></span> <span class="ruby-keyword kw">nil</span>})
|
226
|
-
47: <span class="ruby-keyword kw">end</span></pre>
|
227
|
-
</div>
|
228
|
-
|
229
|
-
</div>
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
</div>
|
235
|
-
|
236
|
-
|
237
213
|
<div id="all-method" class="method-detail ">
|
238
214
|
<a name="method-c-all"></a>
|
239
215
|
|
@@ -248,7 +224,11 @@
|
|
248
224
|
<div class="method-description">
|
249
225
|
|
250
226
|
<p>
|
251
|
-
|
227
|
+
Find all <a href="ParseResource.html">ParseResource</a> objects for that
|
228
|
+
model.
|
229
|
+
</p>
|
230
|
+
<p>
|
231
|
+
@return [Array] an `Array` of objects that subclass `ParseResource`.
|
252
232
|
</p>
|
253
233
|
|
254
234
|
|
@@ -256,12 +236,12 @@ Post.all
|
|
256
236
|
<div class="method-source-code"
|
257
237
|
id="all-source">
|
258
238
|
<pre>
|
259
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
239
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 137</span>
|
240
|
+
137: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">all</span>
|
241
|
+
138: <span class="ruby-identifier">resp</span> = <span class="ruby-identifier">resource</span>.<span class="ruby-identifier">get</span>
|
242
|
+
139: <span class="ruby-identifier">results</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">resp</span>)[<span class="ruby-value str">'results'</span>]
|
243
|
+
140: <span class="ruby-identifier">results</span>.<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span> <span class="ruby-identifier">model_name</span>.<span class="ruby-identifier">constantize</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">r</span>, <span class="ruby-keyword kw">false</span>)}
|
244
|
+
141: <span class="ruby-keyword kw">end</span></pre>
|
265
245
|
</div>
|
266
246
|
|
267
247
|
</div>
|
@@ -292,10 +272,10 @@ Post.all
|
|
292
272
|
<div class="method-source-code"
|
293
273
|
id="class-attributes-source">
|
294
274
|
<pre>
|
295
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
296
|
-
|
297
|
-
|
298
|
-
|
275
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 158</span>
|
276
|
+
158: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">class_attributes</span>
|
277
|
+
159: <span class="ruby-ivar">@class_attributes</span> <span class="ruby-operator">||=</span> {}
|
278
|
+
160: <span class="ruby-keyword kw">end</span></pre>
|
299
279
|
</div>
|
300
280
|
|
301
281
|
</div>
|
@@ -320,7 +300,12 @@ Post.all
|
|
320
300
|
<div class="method-description">
|
321
301
|
|
322
302
|
<p>
|
323
|
-
|
303
|
+
Create a <a href="ParseResource.html">ParseResource</a> object.
|
304
|
+
</p>
|
305
|
+
<p>
|
306
|
+
@param [Hash] attributes a `Hash` of attributes @return [ParseResource] an
|
307
|
+
object that subclasses `ParseResource`. Or returns `false` if object fails
|
308
|
+
to save.
|
324
309
|
</p>
|
325
310
|
|
326
311
|
|
@@ -328,10 +313,11 @@ Post.create(:title => “new post”)
|
|
328
313
|
<div class="method-source-code"
|
329
314
|
id="create-source">
|
330
315
|
<pre>
|
331
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
332
|
-
|
333
|
-
|
334
|
-
|
316
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 147</span>
|
317
|
+
147: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">create</span>(<span class="ruby-identifier">attributes</span> = {})
|
318
|
+
148: <span class="ruby-identifier">attributes</span> = <span class="ruby-constant">HashWithIndifferentAccess</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">attributes</span>)
|
319
|
+
149: <span class="ruby-identifier">new</span>(<span class="ruby-identifier">attributes</span>).<span class="ruby-identifier">save</span>
|
320
|
+
150: <span class="ruby-keyword kw">end</span></pre>
|
335
321
|
</div>
|
336
322
|
|
337
323
|
</div>
|
@@ -355,26 +341,32 @@ Post.create(:title => “new post”)
|
|
355
341
|
|
356
342
|
<div class="method-description">
|
357
343
|
|
358
|
-
|
344
|
+
<p>
|
345
|
+
Explicitly adds a field to the model.
|
346
|
+
</p>
|
347
|
+
<p>
|
348
|
+
@param [Symbol] name the name of the field, eg `:author`. @param [Boolean]
|
349
|
+
val the return value of the field. Only use this within the class.
|
350
|
+
</p>
|
359
351
|
|
360
352
|
|
361
353
|
|
362
354
|
<div class="method-source-code"
|
363
355
|
id="field-source">
|
364
356
|
<pre>
|
365
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
357
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 48</span>
|
358
|
+
48: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">field</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">val</span>=<span class="ruby-keyword kw">nil</span>)
|
359
|
+
49: <span class="ruby-identifier">class_eval</span> <span class="ruby-keyword kw">do</span>
|
360
|
+
50: <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">name</span>) <span class="ruby-keyword kw">do</span>
|
361
|
+
51: <span class="ruby-ivar">@attributes</span>[<span class="ruby-identifier">name</span>] <span class="ruby-operator">?</span> <span class="ruby-ivar">@attributes</span>[<span class="ruby-identifier">name</span>] <span class="ruby-operator">:</span> <span class="ruby-ivar">@unsaved_attributes</span>[<span class="ruby-identifier">name</span>]
|
362
|
+
52: <span class="ruby-keyword kw">end</span>
|
363
|
+
53: <span class="ruby-identifier">define_method</span>(<span class="ruby-node">"#{name}="</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
|
364
|
+
54: <span class="ruby-ivar">@attributes</span>[<span class="ruby-identifier">name</span>] = <span class="ruby-identifier">val</span>
|
365
|
+
55: <span class="ruby-ivar">@unsaved_attributes</span>[<span class="ruby-identifier">name</span>] = <span class="ruby-identifier">val</span>
|
366
|
+
56: <span class="ruby-identifier">val</span>
|
367
|
+
57: <span class="ruby-keyword kw">end</span>
|
368
|
+
58: <span class="ruby-keyword kw">end</span>
|
369
|
+
59: <span class="ruby-keyword kw">end</span></pre>
|
378
370
|
</div>
|
379
371
|
|
380
372
|
</div>
|
@@ -398,17 +390,22 @@ Post.create(:title => “new post”)
|
|
398
390
|
|
399
391
|
<div class="method-description">
|
400
392
|
|
401
|
-
|
393
|
+
<p>
|
394
|
+
Add multiple fields in one line. Same as `#`, but accepts multiple args.
|
395
|
+
</p>
|
396
|
+
<p>
|
397
|
+
@param [Array] *args an array of `Symbol`s, `eg :author, :body, :title`.
|
398
|
+
</p>
|
402
399
|
|
403
400
|
|
404
401
|
|
405
402
|
<div class="method-source-code"
|
406
403
|
id="fields-source">
|
407
404
|
<pre>
|
408
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
409
|
-
|
410
|
-
|
411
|
-
|
405
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 64</span>
|
406
|
+
64: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">fields</span>(*<span class="ruby-identifier">args</span>)
|
407
|
+
65: <span class="ruby-identifier">args</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>(<span class="ruby-identifier">f</span>)}
|
408
|
+
66: <span class="ruby-keyword kw">end</span></pre>
|
412
409
|
</div>
|
413
410
|
|
414
411
|
</div>
|
@@ -433,7 +430,12 @@ Post.create(:title => “new post”)
|
|
433
430
|
<div class="method-description">
|
434
431
|
|
435
432
|
<p>
|
436
|
-
|
433
|
+
Find a <a href="ParseResource.html">ParseResource</a> object by ID
|
434
|
+
</p>
|
435
|
+
<p>
|
436
|
+
@param [String] id the ID of the Parse object you want to find. @return
|
437
|
+
[ParseResource] an object that subclasses <a
|
438
|
+
href="ParseResource.html">ParseResource</a>.
|
437
439
|
</p>
|
438
440
|
|
439
441
|
|
@@ -441,10 +443,10 @@ finders Post.find(“abcdf”)
|
|
441
443
|
<div class="method-source-code"
|
442
444
|
id="find-source">
|
443
445
|
<pre>
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
446
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 120</span>
|
447
|
+
120: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">find</span>(<span class="ruby-identifier">id</span>)
|
448
|
+
121: <span class="ruby-identifier">where</span>(<span class="ruby-value">:objectId</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">id</span>).<span class="ruby-identifier">first</span>
|
449
|
+
122: <span class="ruby-keyword kw">end</span></pre>
|
448
450
|
</div>
|
449
451
|
|
450
452
|
</div>
|
@@ -469,7 +471,7 @@ finders Post.find(“abcdf”)
|
|
469
471
|
<div class="method-description">
|
470
472
|
|
471
473
|
<p>
|
472
|
-
|
474
|
+
Find the first object. Fairly random, not based on any specific condition.
|
473
475
|
</p>
|
474
476
|
|
475
477
|
|
@@ -477,10 +479,10 @@ Post.first
|
|
477
479
|
<div class="method-source-code"
|
478
480
|
id="first-source">
|
479
481
|
<pre>
|
480
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
481
|
-
|
482
|
-
|
483
|
-
|
482
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 154</span>
|
483
|
+
154: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">first</span>
|
484
|
+
155: <span class="ruby-identifier">all</span>.<span class="ruby-identifier">first</span>
|
485
|
+
156: <span class="ruby-keyword kw">end</span></pre>
|
484
486
|
</div>
|
485
487
|
|
486
488
|
</div>
|
@@ -497,25 +499,30 @@ Post.first
|
|
497
499
|
<div class="method-heading">
|
498
500
|
|
499
501
|
<span class="method-name">load!</span><span
|
500
|
-
class="method-args">(
|
502
|
+
class="method-args">(app_id, master_key)</span>
|
501
503
|
<span class="method-click-advice">click to toggle source</span>
|
502
504
|
|
503
505
|
</div>
|
504
506
|
|
505
507
|
<div class="method-description">
|
506
508
|
|
507
|
-
|
509
|
+
<p>
|
510
|
+
Explicitly set Parse.com API keys.
|
511
|
+
</p>
|
512
|
+
<p>
|
513
|
+
@param [String] app_id the Application ID of your Parse database @param
|
514
|
+
[String] master_key the Master Key of your Parse database
|
515
|
+
</p>
|
508
516
|
|
509
517
|
|
510
518
|
|
511
519
|
<div class="method-source-code"
|
512
520
|
id="load--source">
|
513
521
|
<pre>
|
514
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
66: <span class="ruby-keyword kw">end</span></pre>
|
522
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 97</span>
|
523
|
+
97: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">load!</span>(<span class="ruby-identifier">app_id</span>, <span class="ruby-identifier">master_key</span>)
|
524
|
+
98: <span class="ruby-identifier">@@settings</span> = {<span class="ruby-value str">"app_id"</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">app_id</span>, <span class="ruby-value str">"master_key"</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">master_key</span>}
|
525
|
+
99: <span class="ruby-keyword kw">end</span></pre>
|
519
526
|
</div>
|
520
527
|
|
521
528
|
</div>
|
@@ -532,7 +539,7 @@ Post.first
|
|
532
539
|
<div class="method-heading">
|
533
540
|
|
534
541
|
<span class="method-name">new</span><span
|
535
|
-
class="method-args">(attributes = {})</span>
|
542
|
+
class="method-args">(attributes = {}, new=true)</span>
|
536
543
|
<span class="method-click-advice">click to toggle source</span>
|
537
544
|
|
538
545
|
</div>
|
@@ -540,7 +547,12 @@ Post.first
|
|
540
547
|
<div class="method-description">
|
541
548
|
|
542
549
|
<p>
|
543
|
-
|
550
|
+
Instantiates a <a href="ParseResource.html">ParseResource</a> object
|
551
|
+
</p>
|
552
|
+
<p>
|
553
|
+
@params [Hash], [Boolean] a `Hash` of attributes and a `Boolean` that
|
554
|
+
should be false only if the object already exists @return [ParseResource]
|
555
|
+
an object that subclasses `Parseresource`
|
544
556
|
</p>
|
545
557
|
|
546
558
|
|
@@ -548,19 +560,19 @@ instantiation! p = Post.new(:title => “cool story”)
|
|
548
560
|
<div class="method-source-code"
|
549
561
|
id="new-source">
|
550
562
|
<pre>
|
551
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
563
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 31</span>
|
564
|
+
31: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">attributes</span> = {}, <span class="ruby-identifier">new</span>=<span class="ruby-keyword kw">true</span>)
|
565
|
+
32: <span class="ruby-identifier">attributes</span> = <span class="ruby-constant">HashWithIndifferentAccess</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">attributes</span>)
|
566
|
+
33: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">new</span>
|
567
|
+
34: <span class="ruby-ivar">@unsaved_attributes</span> = <span class="ruby-identifier">attributes</span>
|
568
|
+
35: <span class="ruby-keyword kw">else</span>
|
569
|
+
36: <span class="ruby-ivar">@unsaved_attributes</span> = {}
|
570
|
+
37: <span class="ruby-keyword kw">end</span>
|
571
|
+
38: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">attributes</span> = {}
|
572
|
+
39: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">attributes</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">attributes</span>)
|
573
|
+
40: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">attributes</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">attributes</span>.<span class="ruby-identifier">empty?</span>
|
574
|
+
41: <span class="ruby-identifier">create_setters!</span>
|
575
|
+
42: <span class="ruby-keyword kw">end</span></pre>
|
564
576
|
</div>
|
565
577
|
|
566
578
|
</div>
|
@@ -585,7 +597,7 @@ instantiation! p = Post.new(:title => “cool story”)
|
|
585
597
|
<div class="method-description">
|
586
598
|
|
587
599
|
<p>
|
588
|
-
|
600
|
+
Creates a RESTful resource sends requests to [base_uri]/[classname]
|
589
601
|
</p>
|
590
602
|
|
591
603
|
|
@@ -593,18 +605,18 @@ creates a RESTful resource sends requests to [base_uri]/[classname]
|
|
593
605
|
<div class="method-source-code"
|
594
606
|
id="resource-source">
|
595
607
|
<pre>
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 104</span>
|
609
|
+
104: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">resource</span>
|
610
|
+
105: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">@@settings</span>.<span class="ruby-identifier">nil?</span>
|
611
|
+
106: <span class="ruby-identifier">path</span> = <span class="ruby-value str">"config/parse_resource.yml"</span>
|
612
|
+
107: <span class="ruby-identifier">environment</span> = <span class="ruby-keyword kw">defined?</span>(<span class="ruby-constant">Rails</span>) <span class="ruby-operator">&&</span> <span class="ruby-constant">Rails</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:env</span>) <span class="ruby-operator">?</span> <span class="ruby-constant">Rails</span>.<span class="ruby-identifier">env</span> <span class="ruby-operator">:</span> <span class="ruby-constant">ENV</span>[<span class="ruby-value str">"RACK_ENV"</span>]
|
613
|
+
108: <span class="ruby-identifier">@@settings</span> = <span class="ruby-constant">YAML</span>.<span class="ruby-identifier">load</span>(<span class="ruby-constant">ERB</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">path</span>).<span class="ruby-identifier">read</span>).<span class="ruby-identifier">result</span>)[<span class="ruby-identifier">environment</span>]
|
614
|
+
109: <span class="ruby-keyword kw">end</span>
|
615
|
+
110: <span class="ruby-identifier">base_uri</span> = <span class="ruby-node">"https://api.parse.com/1/classes/#{model_name}"</span>
|
616
|
+
111: <span class="ruby-identifier">app_id</span> = <span class="ruby-identifier">@@settings</span>[<span class="ruby-value str">'app_id'</span>]
|
617
|
+
112: <span class="ruby-identifier">master_key</span> = <span class="ruby-identifier">@@settings</span>[<span class="ruby-value str">'master_key'</span>]
|
618
|
+
113: <span class="ruby-constant">RestClient</span><span class="ruby-operator">::</span><span class="ruby-constant">Resource</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">base_uri</span>, <span class="ruby-identifier">app_id</span>, <span class="ruby-identifier">master_key</span>)
|
619
|
+
114: <span class="ruby-keyword kw">end</span></pre>
|
608
620
|
</div>
|
609
621
|
|
610
622
|
</div>
|
@@ -629,8 +641,12 @@ creates a RESTful resource sends requests to [base_uri]/[classname]
|
|
629
641
|
<div class="method-description">
|
630
642
|
|
631
643
|
<p>
|
632
|
-
|
633
|
-
|
644
|
+
Find a <a href="ParseResource.html">ParseResource</a> object by a `Hash` of
|
645
|
+
conditions.
|
646
|
+
</p>
|
647
|
+
<p>
|
648
|
+
@param [Hash] parameters a `Hash` of conditions. @return [Array] an `Array`
|
649
|
+
of objects that subclass `ParseResource`.
|
634
650
|
</p>
|
635
651
|
|
636
652
|
|
@@ -638,12 +654,12 @@ Lorem”)
|
|
638
654
|
<div class="method-source-code"
|
639
655
|
id="where-source">
|
640
656
|
<pre>
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
657
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 128</span>
|
658
|
+
128: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">where</span>(<span class="ruby-identifier">parameters</span>)
|
659
|
+
129: <span class="ruby-identifier">resp</span> = <span class="ruby-identifier">resource</span>.<span class="ruby-identifier">get</span>(<span class="ruby-value">:params</span> =<span class="ruby-operator">></span> {<span class="ruby-value">:where</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">parameters</span>.<span class="ruby-identifier">to_json</span>})
|
660
|
+
130: <span class="ruby-identifier">results</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">resp</span>)[<span class="ruby-value str">'results'</span>]
|
661
|
+
131: <span class="ruby-identifier">results</span>.<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span> <span class="ruby-identifier">model_name</span>.<span class="ruby-identifier">constantize</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">r</span>, <span class="ruby-keyword kw">false</span>)}
|
662
|
+
132: <span class="ruby-keyword kw">end</span></pre>
|
647
663
|
</div>
|
648
664
|
|
649
665
|
</div>
|
@@ -682,11 +698,11 @@ provides access to @attributes for getting and setting
|
|
682
698
|
<div class="method-source-code"
|
683
699
|
id="attributes-source">
|
684
700
|
<pre>
|
685
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
701
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 240</span>
|
702
|
+
240: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">attributes</span>
|
703
|
+
241: <span class="ruby-ivar">@attributes</span> <span class="ruby-operator">||=</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">class_attributes</span>
|
704
|
+
242: <span class="ruby-ivar">@attributes</span>
|
705
|
+
243: <span class="ruby-keyword kw">end</span></pre>
|
690
706
|
</div>
|
691
707
|
|
692
708
|
</div>
|
@@ -717,11 +733,11 @@ provides access to @attributes for getting and setting
|
|
717
733
|
<div class="method-source-code"
|
718
734
|
id="attributes--source">
|
719
735
|
<pre>
|
720
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
736
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 245</span>
|
737
|
+
245: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">attributes=</span>(<span class="ruby-identifier">n</span>)
|
738
|
+
246: <span class="ruby-ivar">@attributes</span> = <span class="ruby-identifier">n</span>
|
739
|
+
247: <span class="ruby-ivar">@attributes</span>
|
740
|
+
248: <span class="ruby-keyword kw">end</span></pre>
|
725
741
|
</div>
|
726
742
|
|
727
743
|
</div>
|
@@ -752,15 +768,16 @@ provides access to @attributes for getting and setting
|
|
752
768
|
<div class="method-source-code"
|
753
769
|
id="create-source">
|
754
770
|
<pre>
|
755
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
771
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 187</span>
|
772
|
+
187: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">create</span>
|
773
|
+
188: <span class="ruby-identifier">resp</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">resource</span>.<span class="ruby-identifier">post</span>(<span class="ruby-ivar">@unsaved_attributes</span>.<span class="ruby-identifier">to_json</span>, <span class="ruby-value">:content_type</span> =<span class="ruby-operator">></span> <span class="ruby-value str">"application/json"</span>)
|
774
|
+
189: <span class="ruby-ivar">@attributes</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">resp</span>))
|
775
|
+
190: <span class="ruby-ivar">@attributes</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-ivar">@unsaved_attributes</span>)
|
776
|
+
191: <span class="ruby-identifier">attributes</span> = <span class="ruby-constant">HashWithIndifferentAccess</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">attributes</span>)
|
777
|
+
192: <span class="ruby-ivar">@unsaved_attributes</span> = {}
|
778
|
+
193: <span class="ruby-identifier">create_setters!</span>
|
779
|
+
194: <span class="ruby-keyword kw">self</span>
|
780
|
+
195: <span class="ruby-keyword kw">end</span></pre>
|
764
781
|
</div>
|
765
782
|
|
766
783
|
</div>
|
@@ -785,8 +802,7 @@ provides access to @attributes for getting and setting
|
|
785
802
|
<div class="method-description">
|
786
803
|
|
787
804
|
<p>
|
788
|
-
|
789
|
-
value”) p.some_attr = “new value“
|
805
|
+
Creates getter and setter methods for model fields
|
790
806
|
</p>
|
791
807
|
|
792
808
|
|
@@ -794,16 +810,26 @@ value”) p.some_attr = “new value“
|
|
794
810
|
<div class="method-source-code"
|
795
811
|
id="create-setters--source">
|
796
812
|
<pre>
|
797
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
813
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 70</span>
|
814
|
+
70: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">create_setters!</span>
|
815
|
+
71: <span class="ruby-ivar">@attributes</span>.<span class="ruby-identifier">each_pair</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span>
|
816
|
+
72: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value">:define_method</span>, <span class="ruby-node">"#{k}="</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
|
817
|
+
73: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">k</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
|
818
|
+
74: <span class="ruby-identifier">k</span> = <span class="ruby-identifier">k</span>.<span class="ruby-identifier">to_s</span>
|
819
|
+
75: <span class="ruby-keyword kw">end</span>
|
820
|
+
76: <span class="ruby-ivar">@attributes</span>[<span class="ruby-identifier">k</span>.<span class="ruby-identifier">to_s</span>] = <span class="ruby-identifier">val</span>
|
821
|
+
77: <span class="ruby-ivar">@unsaved_attributes</span>[<span class="ruby-identifier">k</span>.<span class="ruby-identifier">to_s</span>] = <span class="ruby-identifier">val</span>
|
822
|
+
78: <span class="ruby-identifier">val</span>
|
823
|
+
79: <span class="ruby-keyword kw">end</span>
|
824
|
+
80: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value">:define_method</span>, <span class="ruby-node">"#{k}"</span>) <span class="ruby-keyword kw">do</span>
|
825
|
+
81: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">k</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
|
826
|
+
82: <span class="ruby-identifier">k</span> = <span class="ruby-identifier">k</span>.<span class="ruby-identifier">to_s</span>
|
827
|
+
83: <span class="ruby-keyword kw">end</span>
|
828
|
+
84:
|
829
|
+
85: <span class="ruby-ivar">@attributes</span>[<span class="ruby-identifier">k</span>.<span class="ruby-identifier">to_s</span>]
|
830
|
+
86: <span class="ruby-keyword kw">end</span>
|
831
|
+
87: <span class="ruby-keyword kw">end</span>
|
832
|
+
88: <span class="ruby-keyword kw">end</span></pre>
|
807
833
|
</div>
|
808
834
|
|
809
835
|
</div>
|
@@ -834,8 +860,8 @@ value”) p.some_attr = “new value“
|
|
834
860
|
<div class="method-source-code"
|
835
861
|
id="created-at-source">
|
836
862
|
<pre>
|
837
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
838
|
-
|
863
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 253</span>
|
864
|
+
253: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">created_at</span>; <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">createdAt</span>; <span class="ruby-keyword kw">end</span></pre>
|
839
865
|
</div>
|
840
866
|
|
841
867
|
</div>
|
@@ -866,13 +892,13 @@ value”) p.some_attr = “new value“
|
|
866
892
|
<div class="method-source-code"
|
867
893
|
id="destroy-source">
|
868
894
|
<pre>
|
869
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
895
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 232</span>
|
896
|
+
232: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">destroy</span>
|
897
|
+
233: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">instance_resource</span>.<span class="ruby-identifier">delete</span>
|
898
|
+
234: <span class="ruby-ivar">@attributes</span> = {}
|
899
|
+
235: <span class="ruby-ivar">@unsaved_attributes</span> = {}
|
900
|
+
236: <span class="ruby-keyword kw">nil</span>
|
901
|
+
237: <span class="ruby-keyword kw">end</span></pre>
|
876
902
|
</div>
|
877
903
|
|
878
904
|
</div>
|
@@ -905,8 +931,8 @@ aliasing for idiomatic Ruby
|
|
905
931
|
<div class="method-source-code"
|
906
932
|
id="id-source">
|
907
933
|
<pre>
|
908
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
909
|
-
|
934
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 251</span>
|
935
|
+
251: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">id</span>; <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">objectId</span> <span class="ruby-keyword kw">rescue</span> <span class="ruby-keyword kw">nil</span>; <span class="ruby-keyword kw">end</span></pre>
|
910
936
|
</div>
|
911
937
|
|
912
938
|
</div>
|
@@ -940,51 +966,10 @@ create RESTful resource for the specific Parse object sends requests to
|
|
940
966
|
<div class="method-source-code"
|
941
967
|
id="instance-resource-source">
|
942
968
|
<pre>
|
943
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
</div>
|
948
|
-
|
949
|
-
</div>
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
</div>
|
955
|
-
|
956
|
-
|
957
|
-
<div id="method-missing-method" class="method-detail ">
|
958
|
-
<a name="method-i-method_missing"></a>
|
959
|
-
|
960
|
-
<div class="method-heading">
|
961
|
-
|
962
|
-
<span class="method-name">method_missing</span><span
|
963
|
-
class="method-args">(meth, *args, &block)</span>
|
964
|
-
<span class="method-click-advice">click to toggle source</span>
|
965
|
-
|
966
|
-
</div>
|
967
|
-
|
968
|
-
<div class="method-description">
|
969
|
-
|
970
|
-
<p>
|
971
|
-
another sprinkle of metaprogramming p = Post.new(:some_attr => “some
|
972
|
-
value”) p.some_attr #=> “some value“
|
973
|
-
</p>
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
<div class="method-source-code"
|
978
|
-
id="method-missing-source">
|
979
|
-
<pre>
|
980
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 206</span>
|
981
|
-
206: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">method_missing</span>(<span class="ruby-identifier">meth</span>, *<span class="ruby-identifier">args</span>, &<span class="ruby-identifier">block</span>)
|
982
|
-
207: <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">attributes</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">meth</span>.<span class="ruby-identifier">to_sym</span>)
|
983
|
-
208: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-identifier">meth</span>.<span class="ruby-identifier">to_sym</span>]
|
984
|
-
209: <span class="ruby-keyword kw">else</span>
|
985
|
-
210: <span class="ruby-keyword kw">super</span>
|
986
|
-
211: <span class="ruby-keyword kw">end</span>
|
987
|
-
212: <span class="ruby-keyword kw">end</span></pre>
|
969
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 183</span>
|
970
|
+
183: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">instance_resource</span>
|
971
|
+
184: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">resource</span>[<span class="ruby-node">"#{self.id}"</span>]
|
972
|
+
185: <span class="ruby-keyword kw">end</span></pre>
|
988
973
|
</div>
|
989
974
|
|
990
975
|
</div>
|
@@ -1015,10 +1000,10 @@ value”) p.some_attr #=> “some value“
|
|
1015
1000
|
<div class="method-source-code"
|
1016
1001
|
id="new--source">
|
1017
1002
|
<pre>
|
1018
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1003
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 172</span>
|
1004
|
+
172: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">new?</span>
|
1005
|
+
173: <span class="ruby-operator">!</span><span class="ruby-identifier">persisted?</span>
|
1006
|
+
174: <span class="ruby-keyword kw">end</span></pre>
|
1022
1007
|
</div>
|
1023
1008
|
|
1024
1009
|
</div>
|
@@ -1049,10 +1034,14 @@ value”) p.some_attr #=> “some value“
|
|
1049
1034
|
<div class="method-source-code"
|
1050
1035
|
id="persisted--source">
|
1051
1036
|
<pre>
|
1052
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1037
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 164</span>
|
1038
|
+
164: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">persisted?</span>
|
1039
|
+
165: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">id</span>
|
1040
|
+
166: <span class="ruby-keyword kw">true</span>
|
1041
|
+
167: <span class="ruby-keyword kw">else</span>
|
1042
|
+
168: <span class="ruby-keyword kw">false</span>
|
1043
|
+
169: <span class="ruby-keyword kw">end</span>
|
1044
|
+
170: <span class="ruby-keyword kw">end</span></pre>
|
1056
1045
|
</div>
|
1057
1046
|
|
1058
1047
|
</div>
|
@@ -1085,10 +1074,10 @@ delegate from Class method
|
|
1085
1074
|
<div class="method-source-code"
|
1086
1075
|
id="resource-source">
|
1087
1076
|
<pre>
|
1088
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1077
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 177</span>
|
1078
|
+
177: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">resource</span>
|
1079
|
+
178: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">resource</span>
|
1080
|
+
179: <span class="ruby-keyword kw">end</span></pre>
|
1092
1081
|
</div>
|
1093
1082
|
|
1094
1083
|
</div>
|
@@ -1119,15 +1108,17 @@ delegate from Class method
|
|
1119
1108
|
<div class="method-source-code"
|
1120
1109
|
id="save-source">
|
1121
1110
|
<pre>
|
1122
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1111
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 197</span>
|
1112
|
+
197: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">save</span>
|
1113
|
+
198: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">valid?</span>
|
1114
|
+
199: <span class="ruby-identifier">run_callbacks</span> <span class="ruby-value">:save</span> <span class="ruby-keyword kw">do</span>
|
1115
|
+
200: <span class="ruby-identifier">new?</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">create</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">update</span>
|
1116
|
+
201: <span class="ruby-keyword kw">end</span>
|
1117
|
+
202: <span class="ruby-keyword kw">else</span>
|
1118
|
+
203: <span class="ruby-keyword kw">false</span>
|
1119
|
+
204: <span class="ruby-keyword kw">end</span>
|
1120
|
+
205: <span class="ruby-keyword kw">rescue</span> <span class="ruby-keyword kw">false</span>
|
1121
|
+
206: <span class="ruby-keyword kw">end</span></pre>
|
1131
1122
|
</div>
|
1132
1123
|
|
1133
1124
|
</div>
|
@@ -1144,7 +1135,7 @@ delegate from Class method
|
|
1144
1135
|
<div class="method-heading">
|
1145
1136
|
|
1146
1137
|
<span class="method-name">update</span><span
|
1147
|
-
class="method-args">(attributes =
|
1138
|
+
class="method-args">(attributes = {})</span>
|
1148
1139
|
<span class="method-click-advice">click to toggle source</span>
|
1149
1140
|
|
1150
1141
|
</div>
|
@@ -1158,25 +1149,26 @@ delegate from Class method
|
|
1158
1149
|
<div class="method-source-code"
|
1159
1150
|
id="update-source">
|
1160
1151
|
<pre>
|
1161
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1152
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 208</span>
|
1153
|
+
208: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">update</span>(<span class="ruby-identifier">attributes</span> = {})
|
1154
|
+
209: <span class="ruby-identifier">attributes</span> = <span class="ruby-constant">HashWithIndifferentAccess</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">attributes</span>)
|
1155
|
+
210: <span class="ruby-ivar">@unsaved_attributes</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">attributes</span>)
|
1156
|
+
211:
|
1157
|
+
212: <span class="ruby-identifier">put_attrs</span> = <span class="ruby-ivar">@unsaved_attributes</span>
|
1158
|
+
213: <span class="ruby-identifier">put_attrs</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value str">'objectId'</span>)
|
1159
|
+
214: <span class="ruby-identifier">put_attrs</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value str">'createdAt'</span>)
|
1160
|
+
215: <span class="ruby-identifier">put_attrs</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value str">'updatedAt'</span>)
|
1161
|
+
216: <span class="ruby-identifier">put_attrs</span> = <span class="ruby-identifier">put_attrs</span>.<span class="ruby-identifier">to_json</span>
|
1162
|
+
217:
|
1163
|
+
218: <span class="ruby-identifier">resp</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">instance_resource</span>.<span class="ruby-identifier">put</span>(<span class="ruby-identifier">put_attrs</span>, <span class="ruby-value">:content_type</span> =<span class="ruby-operator">></span> <span class="ruby-value str">"application/json"</span>)
|
1164
|
+
219:
|
1165
|
+
220: <span class="ruby-ivar">@attributes</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">resp</span>))
|
1166
|
+
221: <span class="ruby-ivar">@attributes</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-ivar">@unsaved_attributes</span>)
|
1167
|
+
222: <span class="ruby-ivar">@unsaved_attributes</span> = {}
|
1168
|
+
223: <span class="ruby-identifier">create_setters!</span>
|
1169
|
+
224:
|
1170
|
+
225: <span class="ruby-keyword kw">self</span>
|
1171
|
+
226: <span class="ruby-keyword kw">end</span></pre>
|
1180
1172
|
</div>
|
1181
1173
|
|
1182
1174
|
</div>
|
@@ -1193,7 +1185,7 @@ delegate from Class method
|
|
1193
1185
|
<div class="method-heading">
|
1194
1186
|
|
1195
1187
|
<span class="method-name">update_attributes</span><span
|
1196
|
-
class="method-args">(attributes =
|
1188
|
+
class="method-args">(attributes = {})</span>
|
1197
1189
|
<span class="method-click-advice">click to toggle source</span>
|
1198
1190
|
|
1199
1191
|
</div>
|
@@ -1207,10 +1199,10 @@ delegate from Class method
|
|
1207
1199
|
<div class="method-source-code"
|
1208
1200
|
id="update-attributes-source">
|
1209
1201
|
<pre>
|
1210
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1202
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 228</span>
|
1203
|
+
228: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">update_attributes</span>(<span class="ruby-identifier">attributes</span> = {})
|
1204
|
+
229: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">attributes</span>)
|
1205
|
+
230: <span class="ruby-keyword kw">end</span></pre>
|
1214
1206
|
</div>
|
1215
1207
|
|
1216
1208
|
</div>
|
@@ -1241,8 +1233,8 @@ delegate from Class method
|
|
1241
1233
|
<div class="method-source-code"
|
1242
1234
|
id="updated-at-source">
|
1243
1235
|
<pre>
|
1244
|
-
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line
|
1245
|
-
|
1236
|
+
<span class="ruby-comment cmt"># File lib/parse_resource.rb, line 255</span>
|
1237
|
+
255: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">updated_at</span>; <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">updatedAt</span> <span class="ruby-keyword kw">rescue</span> <span class="ruby-keyword kw">nil</span>; <span class="ruby-keyword kw">end</span></pre>
|
1246
1238
|
</div>
|
1247
1239
|
|
1248
1240
|
</div>
|