ParseModel 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +93 -13
- data/lib/ParseModel/Model.rb +1 -1
- data/lib/ParseModel/User.rb +11 -6
- data/lib/ParseModel/query.rb +34 -7
- data/lib/ParseModel/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -4,10 +4,7 @@ ParseModel provides an Active Record pattern to your Parse models on RubyMotion.
|
|
4
4
|
|
5
5
|
I'm using ParseModel internally for a project, slowly but surely making it much, much better. When the project is near completion, I'm going to extract additional functionality into the gem.
|
6
6
|
|
7
|
-
|
8
|
-
* Easier queries (`Post.where(:author => @author)`)
|
9
|
-
* Associations (`class Post; include Parse::Model; has_one :author; end`)
|
10
|
-
* Overall, a much more Ruby-esque API that still leaves full access to all of the features in the Parse iOS SDK
|
7
|
+
Expect a much more Ruby-esque API that still leaves full access to all of the features in the Parse iOS SDK. I'm not trying to re-implement features from the Parse iOS SDK, I just want to make them easier to use. Moreover, you should be able to rely on [Parse's iOS docs](https://parse.com/docs/ios/api/) when using ParseModel.
|
11
8
|
|
12
9
|
If you have any questions or suggestions, email me.
|
13
10
|
|
@@ -30,11 +27,29 @@ p = Post.new
|
|
30
27
|
p.title = "Why RubyMotion Is Better Than Objective-C"
|
31
28
|
p.author = "Josh Symonds"
|
32
29
|
p.body = "trololol"
|
33
|
-
p.saveEventually
|
34
30
|
```
|
35
31
|
|
36
32
|
`ParseModel::Model` objects will `respond_to?` to all methods available to [`PFObject`](https://parse.com/docs/ios/api/Classes/PFObject.html) in the Parse iOS SDK. You can also access the `PFObject` instance directly with, you guessed it, `ParseModel::Model#PFObject`.
|
37
33
|
|
34
|
+
### Saving objects
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
p = Post.new
|
38
|
+
p.author = "Alan"
|
39
|
+
|
40
|
+
# save using main thread (blocking)
|
41
|
+
p.save
|
42
|
+
|
43
|
+
# save eventually (non-blocking)
|
44
|
+
p.saveEventually
|
45
|
+
|
46
|
+
# save in background with a block
|
47
|
+
p.saveInBackgroundWithBlock(lambda do |success, error|
|
48
|
+
# do something...
|
49
|
+
end)
|
50
|
+
|
51
|
+
```
|
52
|
+
|
38
53
|
### Users
|
39
54
|
|
40
55
|
```ruby
|
@@ -54,22 +69,87 @@ users.map {|u| u.objectId}.include?(user.objectId) #=> true
|
|
54
69
|
|
55
70
|
`ParseModel::User` delegates to `PFUser` in a very similar fashion as `ParseModel::Model` delegates to `PFOBject`.
|
56
71
|
|
57
|
-
|
58
|
-
|
59
|
-
For now, just use Parse's native methods:
|
72
|
+
#### Current User
|
60
73
|
|
61
74
|
```ruby
|
62
|
-
|
63
|
-
|
64
|
-
|
75
|
+
if User.current_user
|
76
|
+
@user = User.current_user
|
77
|
+
end
|
65
78
|
```
|
66
79
|
|
67
|
-
|
80
|
+
### Queries
|
81
|
+
|
82
|
+
Parse provides some great ways to query for objects: in the current blocking thread (`PFQuery#findObjects`, or in the background with a block (`PFQuery#findObjectsInBackGroundWithBlock()`).
|
83
|
+
|
84
|
+
These method names are a little long and verbose for my taste, so I added a little but of syntactic sugar:
|
68
85
|
|
69
86
|
```ruby
|
70
|
-
|
87
|
+
query = Post.query #=> <ParseModel::Query> ... this is a subclass of PFQuery
|
88
|
+
query.whereKey("author", equalTo:"Alan")
|
89
|
+
query.find # finds objects in the main thread, like PFQuery#findObjects
|
90
|
+
|
91
|
+
# Or run the query in a background thread
|
92
|
+
|
93
|
+
query.find do |objects, error|
|
94
|
+
puts "You have #{objects.length} objects of class #{objects.first.class}."
|
95
|
+
end
|
71
96
|
```
|
72
97
|
|
98
|
+
By passing a two-argument block to `ParseModel::Query#find(&block)`, the query will automatically run in the background, with the code from the given block executing on completion.
|
99
|
+
|
100
|
+
Also note that `ParseModel::Query#find` and `ParseModel::Query#find(&block)` return `ParseModel::Model` objects, and not `PFObject`s.
|
101
|
+
|
102
|
+
Because I want Parse's documentation to be as relevant as possible, here's how I'm matching up `ParseModel::Query`'s convenience methods to `PFQuery`:
|
103
|
+
|
104
|
+
<table>
|
105
|
+
<tr>
|
106
|
+
<th>`ParseModel::Query` method</th>
|
107
|
+
<th>Equivalent `PFQuery` method</th>
|
108
|
+
<th>Parse Documentation</th>
|
109
|
+
</tr>
|
110
|
+
<tr>
|
111
|
+
<td>`ParseModel::Query#find`</td>
|
112
|
+
<td>`PFQuery#findObjects`</td>
|
113
|
+
<td><a href='https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/findObjects'>here</a></td>
|
114
|
+
</tr>
|
115
|
+
<tr>
|
116
|
+
<td>`ParseModel::Query#find(&block)`</td>
|
117
|
+
<td>`PFQuery#findObjectsInBackgroundWithBlock`</td>
|
118
|
+
<td><a href='https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/countObjectsInBackgroundWithBlock:'>here</a></td>
|
119
|
+
</tr>
|
120
|
+
<tr>
|
121
|
+
<td>`ParseModel::Query#getFirst`</td>
|
122
|
+
<td>`PFQuery#getFirstObject`</td>
|
123
|
+
<td><a href='https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/getFirstObject'>here</a></td>
|
124
|
+
</tr>
|
125
|
+
<tr>
|
126
|
+
<td>`ParseModel::Query#getFirst(&block)`</td>
|
127
|
+
<td>`PFQuery#getFirstObjectInBackgroundWithBlock`</td>
|
128
|
+
<td><a href='https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/getFirstObjectInBackgroundWithBlock:'>here</a></td>
|
129
|
+
</tr>
|
130
|
+
<tr>
|
131
|
+
<td>`ParseModel::Query#get(id)`</td>
|
132
|
+
<td>`PFQuery#getObjectWithId`</td>
|
133
|
+
<td><a href='https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/getFirstObject'>here</a></td>
|
134
|
+
</tr>
|
135
|
+
<tr>
|
136
|
+
<td>`ParseModel::Query#get(id, &block)`</td>
|
137
|
+
<td>`PFQuery#getObjectInBackgroundWithId:block:`</td>
|
138
|
+
<td><a href='https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/getFirstObjectInBackgroundWithBlock:'>here</a></td>
|
139
|
+
</tr>
|
140
|
+
<tr>
|
141
|
+
<td>`ParseModel::Query#count`</td>
|
142
|
+
<td>`PFQuery#countObjects`</td>
|
143
|
+
<td><a href='https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/countObjects'>here</a></td>
|
144
|
+
</tr>
|
145
|
+
<tr>
|
146
|
+
<td>`ParseModel::Query#count(&block)`</td>
|
147
|
+
<td>`PFQuery#countObjectsInBackgroundWithBlock`</td>
|
148
|
+
<td><a href='https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/countObjectsInBackgroundWithBlock:'>here</a></td>
|
149
|
+
</tr>
|
150
|
+
</table>
|
151
|
+
|
152
|
+
Essentially, I'm omitting the words "object" and "InBackgroundWithBlock" from `ParseModel`'s method signatures. I think it's a reasonable assumption that it can simply be implied that we're dealing with "objects." If I'm passing a block, it's repetitive to declare that I'm passing a block.
|
73
153
|
|
74
154
|
## Installation
|
75
155
|
|
data/lib/ParseModel/Model.rb
CHANGED
data/lib/ParseModel/User.rb
CHANGED
@@ -37,17 +37,22 @@ module ParseModel
|
|
37
37
|
def field(name)
|
38
38
|
@fields
|
39
39
|
end
|
40
|
+
|
41
|
+
def current_user
|
42
|
+
if PFUser.currentUser
|
43
|
+
u = new
|
44
|
+
u.PFUser = PFUser.currentUser
|
45
|
+
return u
|
46
|
+
else
|
47
|
+
return nil
|
48
|
+
end
|
49
|
+
end
|
40
50
|
|
41
51
|
def get_fields
|
42
52
|
@fields ||= []
|
43
53
|
@fields
|
44
54
|
end
|
45
|
-
|
46
|
-
def all
|
47
|
-
query = PFQuery.queryForUser
|
48
|
-
users = query.findObjects
|
49
|
-
users
|
50
|
-
end
|
55
|
+
|
51
56
|
end
|
52
57
|
|
53
58
|
def self.included(base)
|
data/lib/ParseModel/query.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module ParseModel
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
class Query < PFQuery
|
3
|
+
|
4
|
+
def setClassObject(classObject)
|
5
5
|
@classObject = classObject
|
6
6
|
end
|
7
7
|
|
@@ -15,10 +15,37 @@ module ParseModel
|
|
15
15
|
return self.findObjects.map {|obj| @classObject.new(obj)} unless block_given?
|
16
16
|
|
17
17
|
self.findObjectsInBackgroundWithBlock(lambda do |objects, error|
|
18
|
-
|
19
|
-
|
18
|
+
objects = objects.map {|obj| @classObject.new(obj)} if objects
|
19
|
+
block.call(objects, error)
|
20
20
|
end)
|
21
21
|
end
|
22
|
-
|
23
|
-
|
22
|
+
|
23
|
+
def getFirst(&block)
|
24
|
+
return @classObject.new(self.getFirstObject) unless block_given?
|
25
|
+
|
26
|
+
self.getFirstObjectInBackgroundWithBlock(lambda do |object, error|
|
27
|
+
obj = @classObject.new(object) if object
|
28
|
+
block.call(obj, error)
|
29
|
+
end)
|
30
|
+
end
|
31
|
+
|
32
|
+
def get(id, &block)
|
33
|
+
return @classObject.new(self.getObjectWithId(id)) unless block_given?
|
34
|
+
|
35
|
+
self.getObjectInBackgroundWithId(id, block:lambda do |object, error|
|
36
|
+
obj = @classObject.new(object) if object
|
37
|
+
block.call(obj, error)
|
38
|
+
end)
|
39
|
+
end
|
40
|
+
|
41
|
+
def count(&block)
|
42
|
+
return self.countObjects unless block_given?
|
43
|
+
|
44
|
+
self.countObjectsInBackgroundWithBlock(lambda do |count, error|
|
45
|
+
block.call(count, error)
|
46
|
+
end)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
24
51
|
end
|
data/lib/ParseModel/version.rb
CHANGED