lean_motion 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +76 -0
- data/bin/lean_motion +65 -0
- data/lib/lean_motion.rb +17 -0
- data/lib/lean_motion/cloud.rb +9 -0
- data/lib/lean_motion/config.rb +21 -0
- data/lib/lean_motion/lean_motion.rb +3 -0
- data/lib/lean_motion/model.rb +322 -0
- data/lib/lean_motion/query.rb +120 -0
- data/lib/lean_motion/user.rb +137 -0
- data/lib/lean_motion/version.rb +3 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 11a512b0eb4900d63e392c755728747ceaafbabd
|
4
|
+
data.tar.gz: 0edccd1ef76eef1b8790fdb754657b09749e4f29
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 93ef272d5136b2fabc8db74b670093a1cdda48f153aac7e8d3d28a033b6850c1fa08a385e4bd093ac1d20ae94de6bacd7b388caf1f332688c85fbae271a99756
|
7
|
+
data.tar.gz: 71c044f0ec94eef4b4dca0cccf94b3a77bc37c865572a224af2d5bf78286be7c69d6d52789186f9a3201f72744ac0b6cfa8a2eb8b9450e97a44d5a151d828365
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# LeanMotion
|
2
|
+
|
3
|
+
LeanMotion 是一个RubyMotion的Gem,可以更加方便地使用LeanCloud SDK。用更Ruby的写法来操作LeanCloud的数据,类Rails的ActiveRecord,增删查改。
|
4
|
+
|
5
|
+
## 安装说明
|
6
|
+
|
7
|
+
1、安装gem
|
8
|
+
```
|
9
|
+
gem install lean_motion
|
10
|
+
```
|
11
|
+
|
12
|
+
2、创建项目
|
13
|
+
```
|
14
|
+
lean_motion create app-name
|
15
|
+
```
|
16
|
+
|
17
|
+
3、设置LeanCloud的App ID和App Key
|
18
|
+
修改 app_delegate.rb
|
19
|
+
|
20
|
+
```
|
21
|
+
app_id = "your_app_id"
|
22
|
+
app_key = "your_app_key"
|
23
|
+
```
|
24
|
+
|
25
|
+
4、运行
|
26
|
+
```
|
27
|
+
rake
|
28
|
+
```
|
29
|
+
|
30
|
+
## 使用说明
|
31
|
+
1、在LeanCloud后台创建一个Class,比如Product,并添加以下字段
|
32
|
+
```
|
33
|
+
name: String
|
34
|
+
description: String
|
35
|
+
url: String
|
36
|
+
```
|
37
|
+
|
38
|
+
2、添加model文件 product.rb,建议放在app/models/目录下
|
39
|
+
```
|
40
|
+
class Product
|
41
|
+
include LM::Model
|
42
|
+
|
43
|
+
fields :name, :description, :url
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
3、操作数据
|
48
|
+
|
49
|
+
新建产品
|
50
|
+
```
|
51
|
+
product = Product.new
|
52
|
+
product.name = 'iPhone 6'
|
53
|
+
product.description = '目前最好的智能手机'
|
54
|
+
product.url = 'http://www.apple.com'
|
55
|
+
product.save
|
56
|
+
```
|
57
|
+
|
58
|
+
产品数量
|
59
|
+
```
|
60
|
+
Product.count
|
61
|
+
```
|
62
|
+
|
63
|
+
查询产品
|
64
|
+
```
|
65
|
+
Product.where(:name=>'iPhone 6').find
|
66
|
+
```
|
67
|
+
|
68
|
+
获得第一条记录
|
69
|
+
```
|
70
|
+
Product.first
|
71
|
+
```
|
72
|
+
|
73
|
+
排序
|
74
|
+
```
|
75
|
+
Product.sort(:createdAt=>:desc).find
|
76
|
+
```
|
data/bin/lean_motion
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'methadone'
|
5
|
+
require 'lean_motion/version'
|
6
|
+
|
7
|
+
class App
|
8
|
+
include Methadone::Main
|
9
|
+
include Methadone::CLILogging
|
10
|
+
include Methadone::SH
|
11
|
+
|
12
|
+
main do |command, opt|
|
13
|
+
case command.to_sym
|
14
|
+
when :create then create(opt)
|
15
|
+
when :create_pro then create_pro(opt)
|
16
|
+
when :create_user then create_user(opt)
|
17
|
+
when :help then show_help
|
18
|
+
else show_help
|
19
|
+
end
|
20
|
+
|
21
|
+
0 # Good!
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.show_help
|
25
|
+
info "lean_motion - Command line tools for LeanMotion."
|
26
|
+
info "By smartweb"
|
27
|
+
info ""
|
28
|
+
info "Commands:"
|
29
|
+
info " create <appname>"
|
30
|
+
info " Creates a new LeanMotion app from a template."
|
31
|
+
info ""
|
32
|
+
info " create_pro <appname>"
|
33
|
+
info " Creates a new LeanMotion + ProMotion app from a template."
|
34
|
+
info ""
|
35
|
+
info " create_user <appname>"
|
36
|
+
info " Creates a new LeanMotion + ProMotion + User app from a template."
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.create(name)
|
40
|
+
return puts "Usage: lean_motion create <appname>" unless name.to_s.length > 0
|
41
|
+
info "Creating new LeanMotion iOS app #{name}"
|
42
|
+
sh "motion create --template=git://coding.net/smartweb/LeanMotionTemplate.git #{name}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.create_pro(name)
|
46
|
+
return puts "Usage: lean_motion create <appname>" unless name.to_s.length > 0
|
47
|
+
info "Creating new LeanMotion iOS app #{name}"
|
48
|
+
sh "motion create --template=git://coding.net/smartweb/LeanProMotionTemplate.git #{name}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.create_user(name)
|
52
|
+
return puts "Usage: lean_motion create <appname>" unless name.to_s.length > 0
|
53
|
+
info "Creating new LeanMotion iOS app #{name}"
|
54
|
+
sh "motion create --template=git://coding.net/smartweb/LeanUserTemplate.git #{name}"
|
55
|
+
end
|
56
|
+
|
57
|
+
description "Command line for LeanMotion."
|
58
|
+
|
59
|
+
arg :command
|
60
|
+
arg :opt, :optional
|
61
|
+
|
62
|
+
version LeanMotion::VERSION
|
63
|
+
|
64
|
+
go!
|
65
|
+
end
|
data/lib/lean_motion.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
unless defined?(Motion::Project::Config)
|
2
|
+
raise "The lean_motion gem must be required within a RubyMotion project Rakefile."
|
3
|
+
end
|
4
|
+
|
5
|
+
Motion::Project::App.setup do |app|
|
6
|
+
core_lib = File.join(File.dirname(__FILE__), 'lean_motion')
|
7
|
+
insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
|
8
|
+
|
9
|
+
Dir.glob(File.join(core_lib, '**/*.rb')).reverse.each do |file|
|
10
|
+
app.files.insert(insert_point, file)
|
11
|
+
end
|
12
|
+
|
13
|
+
app.vendor_project('vendor/AVOSCloud.framework',
|
14
|
+
:static,
|
15
|
+
:products => ['AVOSCloud'],
|
16
|
+
:headers_dir => 'Headers')
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module LeanMotion
|
2
|
+
class Config
|
3
|
+
|
4
|
+
def self.init(app_id, app_key)
|
5
|
+
if app_id == "your_app_id" || app_key == "your_app_key"
|
6
|
+
NSLog("=========== LeanMotion Error ==========")
|
7
|
+
NSLog("LeanCloud App ID and App Key require")
|
8
|
+
NSLog("=======================================")
|
9
|
+
return
|
10
|
+
end
|
11
|
+
|
12
|
+
AVOSCloud.setApplicationId(app_id, clientKey:app_key)
|
13
|
+
AVOSCloud.useAVCloudCN
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.channel(name)
|
17
|
+
AVAnalytics.setChannel name
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,322 @@
|
|
1
|
+
module LeanMotion
|
2
|
+
module Model
|
3
|
+
attr_accessor :AVObject, :errors
|
4
|
+
|
5
|
+
RESERVED_KEYS = [:objectId, :createdAt, :updatedAt]
|
6
|
+
|
7
|
+
def initialize(av=nil)
|
8
|
+
if av
|
9
|
+
self.AVObject = av
|
10
|
+
else
|
11
|
+
self.AVObject = AVObject.objectWithClassName(self.class.to_s)
|
12
|
+
end
|
13
|
+
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def method_missing(method, *args, &block)
|
18
|
+
method = method.to_sym
|
19
|
+
setter = false
|
20
|
+
|
21
|
+
if setter?(method)
|
22
|
+
setter = true
|
23
|
+
method = method.split("=")[0].to_sym
|
24
|
+
end
|
25
|
+
|
26
|
+
# Setters
|
27
|
+
if RESERVED_KEYS.include?(method) && setter
|
28
|
+
return self.AVObject.send("#{method}=", args)
|
29
|
+
elsif fields.include?(method) && setter
|
30
|
+
return setField(method, args.first)
|
31
|
+
# Getters
|
32
|
+
elsif RESERVED_KEYS.include?(method)
|
33
|
+
return self.AVObject.send(method)
|
34
|
+
elsif fields.include? method
|
35
|
+
return getField(method)
|
36
|
+
elsif self.AVObject.respond_to?("#{method}=")
|
37
|
+
return self.AVObject.send("#{method}=", *args, &block)
|
38
|
+
elsif self.AVObject.respond_to?(method)
|
39
|
+
return self.AVObject.send(method, *args, &block)
|
40
|
+
else
|
41
|
+
super
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def getter?(method)
|
46
|
+
!setter?(method)
|
47
|
+
end
|
48
|
+
|
49
|
+
def setter?(method)
|
50
|
+
method.to_s.include?("=")
|
51
|
+
end
|
52
|
+
|
53
|
+
# Override of ruby's respond_to?
|
54
|
+
#
|
55
|
+
# @param [Symbol] method
|
56
|
+
# @return [Bool] true/false
|
57
|
+
def respond_to?(method)
|
58
|
+
if setter?(method)
|
59
|
+
method = method.to_s.split("=")[0]
|
60
|
+
end
|
61
|
+
|
62
|
+
method = method.to_sym unless method.is_a? Symbol
|
63
|
+
|
64
|
+
return true if fields.include?(method)
|
65
|
+
|
66
|
+
super
|
67
|
+
end
|
68
|
+
|
69
|
+
def fields
|
70
|
+
self.class.send(:get_fields)
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
def getField(field)
|
75
|
+
field = field.to_sym
|
76
|
+
return @AVObject.send(field) if RESERVED_KEYS.include?(field)
|
77
|
+
return @AVObject[field] if fields.include? field
|
78
|
+
raise "AVCloud Exception: Invalid field name #{field} for object #{self.class.to_s}"
|
79
|
+
end
|
80
|
+
|
81
|
+
def setField(field, value)
|
82
|
+
if RESERVED_KEYS.include?(field) || fields.include?(field.to_sym)
|
83
|
+
return @AVObject.removeObjectForKey(field.to_s) if value.nil?
|
84
|
+
return @AVObject.send("#{field}=", value) if RESERVED_KEYS.include?(field)
|
85
|
+
return @AVObject[field] = value
|
86
|
+
else
|
87
|
+
raise "AVCloud Exception: Invalid field name #{field} for object #{self.class.to_s}"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Returns all of the attributes of the Model
|
92
|
+
#
|
93
|
+
# @return [Hash] attributes of Model
|
94
|
+
def attributes
|
95
|
+
attributes = {}
|
96
|
+
fields.each do |f|
|
97
|
+
attributes[f] = getField(f)
|
98
|
+
end
|
99
|
+
@attributes = attributes
|
100
|
+
end
|
101
|
+
|
102
|
+
# Sets the attributes of the Model
|
103
|
+
#
|
104
|
+
# @param [Hash] attrs to set on the Model
|
105
|
+
# @return [Hash] that you gave it
|
106
|
+
# @note will throw an error if a key is invalid
|
107
|
+
def attributes=(attrs)
|
108
|
+
attrs.each do |k, v|
|
109
|
+
if v.respond_to?(:each) && !v.is_a?(AVObject)
|
110
|
+
self.attributes = v
|
111
|
+
elsif self.respond_to? "#{k}="
|
112
|
+
self.send("#{k}=", v)
|
113
|
+
else
|
114
|
+
setField(k, v) unless k.nil?
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Save the current state of the Model to Parse
|
120
|
+
#
|
121
|
+
# @note calls before/after_save hooks
|
122
|
+
# @note before_save MUST return true, or save will not be called on AVObject
|
123
|
+
# @note does not save if validations fail
|
124
|
+
#
|
125
|
+
# @return [Bool] true/false
|
126
|
+
def save
|
127
|
+
saved = false
|
128
|
+
unless before_save == false
|
129
|
+
self.validate
|
130
|
+
|
131
|
+
if @errors && @errors.length > 0
|
132
|
+
saved = false
|
133
|
+
else
|
134
|
+
saved = @AVObject.save
|
135
|
+
end
|
136
|
+
|
137
|
+
after_save if saved
|
138
|
+
end
|
139
|
+
saved
|
140
|
+
end
|
141
|
+
def before_save; end
|
142
|
+
def after_save; end
|
143
|
+
|
144
|
+
def validate
|
145
|
+
reset_errors
|
146
|
+
self.attributes.each do |field, value|
|
147
|
+
validateField field, value
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# Checks to see if the current Model has errors
|
152
|
+
#
|
153
|
+
# @return [Bool] true/false
|
154
|
+
def is_valid?
|
155
|
+
self.validate
|
156
|
+
return false if @errors && @errors.length > 0
|
157
|
+
true
|
158
|
+
end
|
159
|
+
|
160
|
+
def delete
|
161
|
+
deleted = false
|
162
|
+
unless before_delete == false
|
163
|
+
deleted = @AVObject.delete
|
164
|
+
end
|
165
|
+
after_delete if deleted
|
166
|
+
deleted
|
167
|
+
end
|
168
|
+
def before_delete; end
|
169
|
+
def after_delete; end
|
170
|
+
|
171
|
+
|
172
|
+
# Validations
|
173
|
+
def presenceValidations
|
174
|
+
self.class.send(:get_presence_validations)
|
175
|
+
end
|
176
|
+
|
177
|
+
def presenceValidationMessages
|
178
|
+
self.class.send(:get_presence_validation_messages)
|
179
|
+
end
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
def validateField(field, value)
|
184
|
+
@errors ||= {}
|
185
|
+
if presenceValidations.include?(field) && (value.nil? || value == "")
|
186
|
+
messages = presenceValidationMessages
|
187
|
+
if messages.include?(field)
|
188
|
+
@errors[field] = messages[field]
|
189
|
+
else
|
190
|
+
@errors[field] = "#{field} can't be blank"
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def errorForField(field)
|
196
|
+
@errors[field] || false
|
197
|
+
end
|
198
|
+
|
199
|
+
def errors
|
200
|
+
@errors
|
201
|
+
end
|
202
|
+
|
203
|
+
def valid?
|
204
|
+
self.errors.nil? || self.errors.length == 0
|
205
|
+
end
|
206
|
+
|
207
|
+
def invalid?
|
208
|
+
!self.valid?
|
209
|
+
end
|
210
|
+
|
211
|
+
def reset_errors
|
212
|
+
@errors = nil
|
213
|
+
end
|
214
|
+
|
215
|
+
module ClassMethods
|
216
|
+
|
217
|
+
def query
|
218
|
+
LeanMotion::CloudQuery.alloc.initWithClassNameAndClassObject(self.name, classObject:self)
|
219
|
+
end
|
220
|
+
|
221
|
+
def count(&block)
|
222
|
+
cloud_query = query
|
223
|
+
return cloud_query.countObjects unless block_given?
|
224
|
+
|
225
|
+
cloud_query.count do |count, error|
|
226
|
+
block.call(count, error)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
def all(&block)
|
231
|
+
cloud_query = query
|
232
|
+
return cloud_query.find unless block_given?
|
233
|
+
|
234
|
+
cloud_query.find do |objects, error|
|
235
|
+
block.call(objects, error)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
def where(hash)
|
240
|
+
cloud_query = query
|
241
|
+
cloud_query.findByHash(hash)
|
242
|
+
end
|
243
|
+
|
244
|
+
def first(&block)
|
245
|
+
cloud_query = query
|
246
|
+
return cloud_query.first unless block_given?
|
247
|
+
|
248
|
+
cloud_query.first do |object, error|
|
249
|
+
block.call(object, error)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
def destroy(&block)
|
254
|
+
cloud_query = query
|
255
|
+
return cloud_query.delete unless block_given?
|
256
|
+
|
257
|
+
cloud_query.destroy do |object, error|
|
258
|
+
block.call(object, error)
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
def sort(hash)
|
263
|
+
cloud_query = query
|
264
|
+
cloud_query.sort(hash)
|
265
|
+
end
|
266
|
+
|
267
|
+
def page(number, pagesize=20)
|
268
|
+
cloud_query = query
|
269
|
+
cloud_query.page(number, pagesize)
|
270
|
+
end
|
271
|
+
|
272
|
+
# set the fields for the current Model
|
273
|
+
# used in method_missing
|
274
|
+
#
|
275
|
+
# @param [Symbol] args one or more fields
|
276
|
+
def fields(*args)
|
277
|
+
args.each {|arg| field(arg)}
|
278
|
+
end
|
279
|
+
|
280
|
+
# set a field for the current Model
|
281
|
+
#
|
282
|
+
# @param [Symbol] name of field
|
283
|
+
# (see #fields)
|
284
|
+
def field(name)
|
285
|
+
@fields ||= [:objectId]
|
286
|
+
@fields << name.to_sym
|
287
|
+
@fields.uniq!
|
288
|
+
end
|
289
|
+
|
290
|
+
def get_fields
|
291
|
+
@fields ||= []
|
292
|
+
end
|
293
|
+
|
294
|
+
# require a certain field to be present (not nil And not an empty String)
|
295
|
+
#
|
296
|
+
# @param [Symbol, Hash] field and options (now only has message)
|
297
|
+
def validates_presence_of(field, opts={})
|
298
|
+
@presenceValidationMessages ||= {}
|
299
|
+
@presenceValidationMessages[field] = opts[:message] if opts[:message]
|
300
|
+
validate_presence(field)
|
301
|
+
end
|
302
|
+
|
303
|
+
def get_presence_validations
|
304
|
+
@presenceValidations ||= {}
|
305
|
+
end
|
306
|
+
|
307
|
+
def get_presence_validation_messages
|
308
|
+
@presenceValidationMessages ||= {}
|
309
|
+
end
|
310
|
+
|
311
|
+
def validate_presence(field)
|
312
|
+
@presenceValidations ||= []
|
313
|
+
@presenceValidations << field
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
def self.included(base)
|
318
|
+
base.extend(ClassMethods)
|
319
|
+
end
|
320
|
+
|
321
|
+
end
|
322
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module LeanMotion
|
2
|
+
class CloudQuery < AVQuery
|
3
|
+
|
4
|
+
def setClassObject(classObject)
|
5
|
+
@classObject = classObject
|
6
|
+
self
|
7
|
+
end
|
8
|
+
|
9
|
+
def initWithClassNameAndClassObject(className, classObject:myClassObject)
|
10
|
+
@className = className
|
11
|
+
self.initWithClassName(className)
|
12
|
+
self.setClassObject(myClassObject)
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def findByHash(hash)
|
17
|
+
hash.each do |k, v|
|
18
|
+
symbols = [:gt, :greater, :lt, :less, :gte, :greater_and_equal,
|
19
|
+
:lte, :less_and_equal, :bt, :between]
|
20
|
+
|
21
|
+
if v.class == Array && (symbols.include? v[0])
|
22
|
+
compare(k, v)
|
23
|
+
else
|
24
|
+
equal(k, v)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def equal(k, v)
|
31
|
+
if v.nil?
|
32
|
+
self.whereKeyDoesNotExist(k)
|
33
|
+
elsif v == :exist
|
34
|
+
self.whereKeyExists(k)
|
35
|
+
else
|
36
|
+
self.whereKey(k, equalTo:v)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def compare(k, v)
|
41
|
+
type = v[0]
|
42
|
+
value = v[1]
|
43
|
+
case type
|
44
|
+
when :gt, :greater
|
45
|
+
self.whereKey(k, greaterThan: value)
|
46
|
+
when :lt, :less
|
47
|
+
self.whereKey(k, lessThan: value)
|
48
|
+
when :gte, :greater_and_equal
|
49
|
+
self.whereKey(k, greaterThanOrEqualTo: value)
|
50
|
+
when :lte, :less_and_equal
|
51
|
+
self.whereKey(k, lessThanOrEqualTo: value)
|
52
|
+
when :bt, :between
|
53
|
+
self.whereKey(k, greaterThan: v[1])
|
54
|
+
self.whereKey(k, lessThan: v[2])
|
55
|
+
end
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def find(&block)
|
60
|
+
return self.findObjects.map {|obj| @classObject.new(obj)} unless block_given?
|
61
|
+
|
62
|
+
self.findObjectsInBackgroundWithBlock(lambda do |objects, error|
|
63
|
+
objects = objects.map {|obj| @classObject.new(obj)} if objects
|
64
|
+
block.call(objects, error)
|
65
|
+
end)
|
66
|
+
end
|
67
|
+
|
68
|
+
def first(&block)
|
69
|
+
return @classObject.new(self.getFirstObject) unless block_given?
|
70
|
+
|
71
|
+
self.getFirstObjectInBackgroundWithBlock(lambda do |object, error|
|
72
|
+
obj = @classObject.new(object) if object
|
73
|
+
block.call(obj, error)
|
74
|
+
end)
|
75
|
+
end
|
76
|
+
|
77
|
+
def get(id, &block)
|
78
|
+
return @classObject.new(self.getObjectWithId(id)) unless block_given?
|
79
|
+
|
80
|
+
self.getObjectInBackgroundWithId(id, block:lambda do |object, error|
|
81
|
+
obj = @classObject.new(object) if object
|
82
|
+
block.call(obj, error)
|
83
|
+
end)
|
84
|
+
end
|
85
|
+
|
86
|
+
def count(&block)
|
87
|
+
return self.countObjects unless block_given?
|
88
|
+
|
89
|
+
self.countObjectsInBackgroundWithBlock(lambda do |count, error|
|
90
|
+
block.call(count, error)
|
91
|
+
end)
|
92
|
+
end
|
93
|
+
|
94
|
+
def destroy(&block)
|
95
|
+
self.deleteAllInBackgroundWithBlock(lambda do |objects, error|
|
96
|
+
block.call(objects, error)
|
97
|
+
end)
|
98
|
+
end
|
99
|
+
|
100
|
+
def page(number, pagesize=20)
|
101
|
+
number ||= 1
|
102
|
+
self.limit = pagesize
|
103
|
+
self.skip = (number - 1) * pagesize
|
104
|
+
self
|
105
|
+
end
|
106
|
+
|
107
|
+
def sort(hash)
|
108
|
+
key = hash.first[0]
|
109
|
+
sort= hash.first[1]
|
110
|
+
if sort == :asc
|
111
|
+
self.orderByAscending(key)
|
112
|
+
else
|
113
|
+
self.orderByDescending(key)
|
114
|
+
end
|
115
|
+
self
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
module LeanMotion
|
2
|
+
module User
|
3
|
+
attr_accessor :AVUser
|
4
|
+
|
5
|
+
RESERVED_KEYS = ['username', 'password', 'email']
|
6
|
+
|
7
|
+
def initialize(av_user_object=nil)
|
8
|
+
if av_user_object
|
9
|
+
@AVUser = av_user_object
|
10
|
+
else
|
11
|
+
@AVUser = AVUser.user
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(method, *args, &block)
|
16
|
+
if RESERVED_KEYS.include?("#{method}")
|
17
|
+
@AVUser.send(method)
|
18
|
+
elsif RESERVED_KEYS.map {|f| "#{f}="}.include?("#{method}")
|
19
|
+
@AVUser.send(method, args.first)
|
20
|
+
elsif fields.include?(method)
|
21
|
+
@AVUser.objectForKey(method)
|
22
|
+
elsif fields.map {|f| "#{f}="}.include?("#{method}")
|
23
|
+
method = method.split("=")[0]
|
24
|
+
@AVUser.setObject(args.first, forKey:method)
|
25
|
+
elsif @AVUser.respond_to?(method)
|
26
|
+
@AVUser.send(method, *args, &block)
|
27
|
+
else
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def fields
|
33
|
+
self.class.send(:get_fields)
|
34
|
+
end
|
35
|
+
|
36
|
+
def sign(&block)
|
37
|
+
return true unless block_given?
|
38
|
+
@AVUser.signUpInBackgroundWithBlock(lambda do |succeeded, error|
|
39
|
+
block.call(succeeded, error)
|
40
|
+
end)
|
41
|
+
end
|
42
|
+
|
43
|
+
module ClassMethods
|
44
|
+
|
45
|
+
def login(username, password, &block)
|
46
|
+
AVUser.logInWithUsernameInBackground(username, password:password, block:lambda do |user, error|
|
47
|
+
block.call(user, error)
|
48
|
+
end)
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def query
|
53
|
+
LeanMotion::CloudQuery.alloc.initWithClassNameAndClassObject('_User', classObject:self)
|
54
|
+
end
|
55
|
+
|
56
|
+
def count(&block)
|
57
|
+
cloud_query = query
|
58
|
+
return cloud_query.countObjects unless block_given?
|
59
|
+
|
60
|
+
cloud_query.countObjectsInBackgroundWithBlock(lambda do |count, error|
|
61
|
+
block.call(count, error)
|
62
|
+
end)
|
63
|
+
end
|
64
|
+
|
65
|
+
def where(hash)
|
66
|
+
cloud_query = query
|
67
|
+
cloud_query.findByHash(hash)
|
68
|
+
end
|
69
|
+
|
70
|
+
def first(&block)
|
71
|
+
cloud_query = query
|
72
|
+
return cloud_query.getFirst unless block_given?
|
73
|
+
|
74
|
+
cloud_query.getFirstObjectInBackgroundWithBlock(lambda do |object, error|
|
75
|
+
block.call(object, error)
|
76
|
+
end)
|
77
|
+
end
|
78
|
+
|
79
|
+
def order(hash)
|
80
|
+
cloud_query = query
|
81
|
+
key = hash.first[0]
|
82
|
+
sort= hash.first[1]
|
83
|
+
if sort == :asc
|
84
|
+
cloud_query.orderByAscending(key)
|
85
|
+
else
|
86
|
+
cloud_query.orderByDescending(key)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def page(number, pagesize=20)
|
91
|
+
number ||= 1
|
92
|
+
cloud_query = query
|
93
|
+
cloud_query.limit = pagesize
|
94
|
+
cloud_query.skip = (number - 1) * pagesize
|
95
|
+
cloud_query
|
96
|
+
end
|
97
|
+
|
98
|
+
def fields(*args)
|
99
|
+
args.each {|arg| field(arg)}
|
100
|
+
end
|
101
|
+
|
102
|
+
def field(name)
|
103
|
+
@fields ||= []
|
104
|
+
@fields << name
|
105
|
+
end
|
106
|
+
|
107
|
+
def current_user
|
108
|
+
if AVUser.currentUser
|
109
|
+
u = new
|
110
|
+
u.AVUser = AVUser.currentUser
|
111
|
+
return u
|
112
|
+
else
|
113
|
+
return nil
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def all(&block)
|
118
|
+
return AVUser.query.findObjects.map {|obj| self.new(obj)} unless block_given?
|
119
|
+
|
120
|
+
AVUser.query.findObjectsInBackgroundWithBlock(lambda do |objects, error|
|
121
|
+
objects = objects.map {|obj| self.new(obj)} if objects
|
122
|
+
block.call(objects, error)
|
123
|
+
end)
|
124
|
+
end
|
125
|
+
|
126
|
+
def get_fields
|
127
|
+
@fields
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
def self.included(base)
|
133
|
+
base.extend(ClassMethods)
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lean_motion
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- smartweb
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: methadone
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Make it more easy to use LeanCloud SDK in RubyMotion. Inspired by ParseModel.
|
42
|
+
email: sam@chamobile.com
|
43
|
+
executables:
|
44
|
+
- lean_motion
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- README.md
|
49
|
+
- bin/lean_motion
|
50
|
+
- lib/lean_motion.rb
|
51
|
+
- lib/lean_motion/cloud.rb
|
52
|
+
- lib/lean_motion/config.rb
|
53
|
+
- lib/lean_motion/lean_motion.rb
|
54
|
+
- lib/lean_motion/model.rb
|
55
|
+
- lib/lean_motion/query.rb
|
56
|
+
- lib/lean_motion/user.rb
|
57
|
+
- lib/lean_motion/version.rb
|
58
|
+
homepage: http://github.com/smartweb/lean_motion
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.4.2
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Rubymotion wrapper for LeanCloud SDK
|
82
|
+
test_files: []
|