pureapi 0.1.0 → 0.1.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 +4 -4
- data/lib/pureapi/controller.rb +14 -12
- data/lib/pureapi/model.rb +34 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 628bbd91dbfc41389e809cae66cfb3d54bba240d
|
4
|
+
data.tar.gz: e03758fcb81833fbb791489a55241dbe1326459e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4af6e348397ef06958d1db53fc57a113743b79c771dc434e424e322dfcb23785083f5cd3229e35ddbd3ecfdce74179f397daa5ffa5981ee349052bf784e1c9cb
|
7
|
+
data.tar.gz: d77d76005ffad94bf19c3b72c56a331874fb13df9ff14effc1019c0a1c275265da2eca732a6fba08dc9c94e20f0ae57f69b189e93e9a3da03744cbcf03b028ce
|
data/lib/pureapi/controller.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'pureapi/model'
|
2
|
+
|
1
3
|
# Implement methods use for handle with table records like
|
2
4
|
# 1. Pagination
|
3
5
|
# 2. Sort
|
@@ -198,7 +200,7 @@ module Pureapi::Controller
|
|
198
200
|
compcond_params = []
|
199
201
|
|
200
202
|
objects.compcond_columns.each do |column_name|
|
201
|
-
|
203
|
+
Pureapi::Model::COMPARISON_OPERATORS.each do |key, value|
|
202
204
|
compcond_params << [column_name, key].join('.')
|
203
205
|
end
|
204
206
|
end
|
@@ -207,16 +209,16 @@ module Pureapi::Controller
|
|
207
209
|
params[:compconds] = {}
|
208
210
|
|
209
211
|
compconds.each do |compcond|
|
210
|
-
params[:compconds]["#{compcond[:f]}.#{
|
212
|
+
params[:compconds]["#{compcond[:f]}.#{Pureapi::Model::COMPARISON_OPERATORS_INVERT[compcond[:o]]}"] = compcond[:v]
|
211
213
|
end
|
212
214
|
|
213
215
|
objects.compconds(compconds)
|
214
216
|
end
|
215
217
|
|
216
218
|
# Not use under code because lte => lt
|
217
|
-
# matchs = /([a-z0-9_]+)\.(#{
|
219
|
+
# matchs = /([a-z0-9_]+)\.(#{Pureapi::Model::COMPARISON_OPERATORS.keys.join('|')})/.match(key)
|
218
220
|
# if matchs && column_names.include?(matchs[1])
|
219
|
-
# results << {f: matchs[1], o:
|
221
|
+
# results << {f: matchs[1], o: Pureapi::Model::COMPARISON_OPERATORS[matchs[2].to_sym], v: value}
|
220
222
|
# end
|
221
223
|
def parse_compconds(compconds = {}, column_names = [])
|
222
224
|
results = []
|
@@ -225,13 +227,13 @@ module Pureapi::Controller
|
|
225
227
|
splits = key.split('.')
|
226
228
|
|
227
229
|
if splits.length == 2 && column_names.include?(splits[0])
|
228
|
-
if splits[1].to_sym == :eq &&
|
230
|
+
if splits[1].to_sym == :eq && Pureapi::Model::NULL_OPERATOR_KEYS.include?(value)
|
229
231
|
results << {
|
230
232
|
f: splits[0],
|
231
|
-
o:
|
232
|
-
v:
|
233
|
+
o: Pureapi::Model::COMPARISON_OPERATORS[splits[1].to_sym],
|
234
|
+
v: Pureapi::Model::NULL_OPERATORS[value]}
|
233
235
|
else
|
234
|
-
results << {f: splits[0], o:
|
236
|
+
results << {f: splits[0], o: Pureapi::Model::COMPARISON_OPERATORS[splits[1].to_sym], v: value}
|
235
237
|
end
|
236
238
|
end
|
237
239
|
end unless compconds.blank?
|
@@ -254,7 +256,7 @@ module Pureapi::Controller
|
|
254
256
|
params[:logics] = {}
|
255
257
|
|
256
258
|
logics.each do |logic|
|
257
|
-
params[:logics]["#{logic[:f]}.#{
|
259
|
+
params[:logics]["#{logic[:f]}.#{Pureapi::Model::LOGICAL_OPERATORS_INVERT[logic[:o]]}"] = logic[:v]
|
258
260
|
end
|
259
261
|
|
260
262
|
objects.logicconds(logics)
|
@@ -271,7 +273,7 @@ module Pureapi::Controller
|
|
271
273
|
splits = key.split('.')
|
272
274
|
|
273
275
|
if splits.length == 2 && column_names.include?(splits[0])
|
274
|
-
results << {f: splits[0], o:
|
276
|
+
results << {f: splits[0], o: Pureapi::Model::LOGICAL_OPERATORS[splits[1].to_sym], v: value}
|
275
277
|
end
|
276
278
|
end unless compconds.blank?
|
277
279
|
|
@@ -289,11 +291,11 @@ module Pureapi::Controller
|
|
289
291
|
# Criteria model that will be pagination
|
290
292
|
# Return criteria_model.paginate and params[:page, :per_page, :page_total]
|
291
293
|
def paging_standard(criteria_model)
|
292
|
-
per_page = [paging_params[:per_page].try(:to_i) ||
|
294
|
+
per_page = [paging_params[:per_page].try(:to_i) || Pureapi::Model::Pagination::DEFAULT_PER_PAGE, 1].max
|
293
295
|
|
294
296
|
# Paging standard
|
295
297
|
record_total = criteria_model.count
|
296
|
-
per_page = record_total if paging_params[:per_page] ==
|
298
|
+
per_page = record_total if paging_params[:per_page] == Pureapi::Model::Pagination::INFINITE_PER_PAGE\
|
297
299
|
&& record_total > 0
|
298
300
|
page_total = (record_total.to_f / per_page).ceil
|
299
301
|
page = validate_page(paging_params[:page].try(:to_i) || 1, page_total)
|
data/lib/pureapi/model.rb
CHANGED
@@ -2,6 +2,38 @@
|
|
2
2
|
# 1. Methods in as_json
|
3
3
|
# 2.
|
4
4
|
module Pureapi::Model
|
5
|
+
# Define constants use for pure api
|
6
|
+
module Pagination
|
7
|
+
DEFAULT_PER_PAGE = 10
|
8
|
+
INFINITE_PER_PAGE = 'infinite'.freeze
|
9
|
+
end
|
10
|
+
|
11
|
+
COMPARISON_OPERATORS = {
|
12
|
+
eq: '=',
|
13
|
+
ne: '<>',
|
14
|
+
gt: '>',
|
15
|
+
lt: '<',
|
16
|
+
gte: '>=',
|
17
|
+
lte: '<=',
|
18
|
+
like: 'LIKE',
|
19
|
+
nlike: 'NOT LIKE',
|
20
|
+
regex: 'REGEXP',
|
21
|
+
}
|
22
|
+
|
23
|
+
NULL_OPERATORS = {
|
24
|
+
'null' => nil,
|
25
|
+
'blank' => [nil, ''],
|
26
|
+
}
|
27
|
+
|
28
|
+
LOGICAL_OPERATORS = {
|
29
|
+
in: 'IN'
|
30
|
+
}
|
31
|
+
|
32
|
+
COMPARISON_OPERATORS_INVERT = COMPARISON_OPERATORS.invert
|
33
|
+
LOGICAL_OPERATORS_INVERT = LOGICAL_OPERATORS.invert
|
34
|
+
NULL_OPERATOR_KEYS = NULL_OPERATORS.keys
|
35
|
+
|
36
|
+
|
5
37
|
# return default array contains methods for as_json
|
6
38
|
# Eg. [:status_name, :path_detail, :restrictions, :channel_course_code]
|
7
39
|
def json_methods
|
@@ -62,7 +94,7 @@ module Pureapi::Model
|
|
62
94
|
criterias = self
|
63
95
|
|
64
96
|
params.each do |param|
|
65
|
-
if param[:o] ==
|
97
|
+
if param[:o] == COMPARISON_OPERATORS[:eq]
|
66
98
|
criterias = criterias.where(param[:f] => param[:v])
|
67
99
|
else
|
68
100
|
criterias = criterias.where("`#{self.table_name}`.`#{param[:f]}` #{param[:o]} ?", param[:v])
|
@@ -81,7 +113,7 @@ module Pureapi::Model
|
|
81
113
|
criterias = self
|
82
114
|
|
83
115
|
params.each do |param|
|
84
|
-
if param[:o] ==
|
116
|
+
if param[:o] == LOGICAL_OPERATORS[:in]
|
85
117
|
criterias = criterias.where(param[:f] => param[:v])
|
86
118
|
end
|
87
119
|
end
|