getto-params 1.2.0 → 1.3.0
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/.release-version +1 -1
- data/CHANGELOG.md +4 -0
- data/CHANGELOG/1.3.0.md +13 -0
- data/Gemfile.lock +1 -1
- data/README.md +109 -1
- data/lib/getto/params/search.rb +85 -19
- data/lib/getto/params/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6040b7167c38d715e98f1556713c86d809e8fa61e10e10f17cabb92c6045b7ac
|
4
|
+
data.tar.gz: 4ef9fca6bcb60ad1eae3e72145871a226d962f7ffae1e0d37edd33af79430573
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e291c389da36f6882acd05cac84b4ba9b35dc669f38e907527e9194d979ac65ebc9efae0513f14db6a9563a8112080d3b4b3d72c6681720a4187a0e8ad1a66f
|
7
|
+
data.tar.gz: daac1469c983748c45f87ca87597478260b53d146bb323d0966465989bb064eb94e434768c2967448b7d03040eb9b8634b421b4ed8ed867bd8da4126e841cc20
|
data/.release-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.3.0
|
data/CHANGELOG.md
CHANGED
data/CHANGELOG/1.3.0.md
ADDED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -17,6 +17,8 @@ Getto::Params.new.validate(params) do |v|
|
|
17
17
|
"str1" => v.in(["param1","param2"]),
|
18
18
|
"str2" => v.in(["param1","param2"]),
|
19
19
|
"tel" => v.combine([v.string, v.match(%r{\A[0-9]+-[0-9]+-[0-9]+\Z})]),
|
20
|
+
"date" => v.combine([v.string, v.match_date]),
|
21
|
+
"data" => v.not_nil,
|
20
22
|
"number" => v.match_integer,
|
21
23
|
"bool" => v.match_bool,
|
22
24
|
"hash" => v.hash(
|
@@ -42,6 +44,8 @@ end
|
|
42
44
|
```ruby
|
43
45
|
require "getto/params/search"
|
44
46
|
|
47
|
+
time = Time # respond to `parse`
|
48
|
+
|
45
49
|
Getto::Params::Search.new(
|
46
50
|
page: 1,
|
47
51
|
limit: 1000,
|
@@ -49,14 +53,28 @@ Getto::Params::Search.new(
|
|
49
53
|
query: {
|
50
54
|
"name.cont" => "search",
|
51
55
|
"value.eq" => "value1",
|
56
|
+
"date.lteq" => "2018-10-01",
|
57
|
+
"time.gteq" => "2018-10-01",
|
58
|
+
"time.lteq" => "2018-10-01",
|
52
59
|
},
|
53
60
|
).to_h do |search|
|
54
61
|
search.sort do |s|
|
55
62
|
s.straight :name
|
56
63
|
end
|
64
|
+
|
65
|
+
search.convert do |c|
|
66
|
+
c.convert "date.lteq", &c.to_date
|
67
|
+
c.convert "time.gteq", &c.to_beginning_of_day(time)
|
68
|
+
c.convert "time.lteq", &c.to_end_of_day(time)
|
69
|
+
end
|
70
|
+
|
57
71
|
search.query do |q|
|
58
72
|
q.search "name.cont", &q.not_empty
|
59
|
-
q.search("
|
73
|
+
q.search("value.eq"){|val| ["value1","value2"].include? val }
|
74
|
+
|
75
|
+
q.search "date.lteq", &q.not_nil
|
76
|
+
q.search "time.gteq", &q.not_nil
|
77
|
+
q.search "time.lteq", &q.not_nil
|
60
78
|
end
|
61
79
|
end
|
62
80
|
# => {
|
@@ -69,6 +87,9 @@ end
|
|
69
87
|
# query: {
|
70
88
|
# "name.cont": "search",
|
71
89
|
# "value.eq": "value1",
|
90
|
+
# "date.lteq": Date.parse("2018-10-01"),
|
91
|
+
# "time.gteq": Time.parse("2018-10-01 00:00:00"),
|
92
|
+
# "time.lteq": Time.parse("2018-10-01 23:59:59"),
|
72
93
|
# },
|
73
94
|
# }
|
74
95
|
```
|
@@ -126,6 +147,9 @@ Getto::Params.new.validate(params) do |v|
|
|
126
147
|
# downcase should be equal to "true" or "false"
|
127
148
|
"key" => v.match_bool,
|
128
149
|
|
150
|
+
# should match date
|
151
|
+
"key" => v.match_date,
|
152
|
+
|
129
153
|
|
130
154
|
# should be hash includes :key that value is string
|
131
155
|
"key" => v.hash(
|
@@ -152,6 +176,9 @@ Getto::Params.new.validate(params) do |v|
|
|
152
176
|
# validate string and not_empty
|
153
177
|
"key" => v.combine([v.string, v.not_empty]),
|
154
178
|
|
179
|
+
# validate not_nil
|
180
|
+
"key" => v.not_nil,
|
181
|
+
|
155
182
|
|
156
183
|
# raise error if valudation failed
|
157
184
|
"key" => v.string{|val| raise ArgumentError, "key should be string: #{val}" }
|
@@ -169,6 +196,8 @@ Format parameters for search api
|
|
169
196
|
```ruby
|
170
197
|
require "getto/params/search"
|
171
198
|
|
199
|
+
time = Time # respond to `parse`
|
200
|
+
|
172
201
|
Getto::Params::Search.new(
|
173
202
|
page: 1,
|
174
203
|
limit: 1000,
|
@@ -186,6 +215,12 @@ Getto::Params::Search.new(
|
|
186
215
|
s.invert :name
|
187
216
|
end
|
188
217
|
|
218
|
+
search.convert do |c|
|
219
|
+
c.convert "date.lteq", &c.to_date
|
220
|
+
c.convert "time.gteq", &c.to_beginning_of_day(time)
|
221
|
+
c.convert "time.lteq", &c.to_end_of_day(time)
|
222
|
+
end
|
223
|
+
|
189
224
|
search.query do |q|
|
190
225
|
# search "name.cont" if value not empty
|
191
226
|
q.search "name.cont", &q.not_empty
|
@@ -257,6 +292,62 @@ end
|
|
257
292
|
# }
|
258
293
|
```
|
259
294
|
|
295
|
+
#### convert query
|
296
|
+
|
297
|
+
- to date
|
298
|
+
|
299
|
+
```ruby
|
300
|
+
search.convert do |c|
|
301
|
+
c.convert "date.lteq", &c.to_date
|
302
|
+
end
|
303
|
+
|
304
|
+
# query: { "date.lteq" => "invalid date" }
|
305
|
+
# => query: {
|
306
|
+
# "date.lteq" => nil,
|
307
|
+
# }
|
308
|
+
|
309
|
+
# query: { "date.lteq" => "2018-10-01" }
|
310
|
+
# => query: {
|
311
|
+
# "date.lteq" => Date.parse("2018-10-01"),
|
312
|
+
# }
|
313
|
+
```
|
314
|
+
|
315
|
+
- to beginning of day, to end of day
|
316
|
+
|
317
|
+
```ruby
|
318
|
+
time = Time # respond to `parse`
|
319
|
+
|
320
|
+
search.convert do |c|
|
321
|
+
c.convert "time.gteq", &c.to_beginning_of_day(time)
|
322
|
+
c.convert "time.lteq", &c.to_end_of_day(time)
|
323
|
+
end
|
324
|
+
|
325
|
+
# query: { "time.gteq" => "invalid date", "time.lteq" => "invalid date" }
|
326
|
+
# => query: {
|
327
|
+
# "time.gteq" => nil,
|
328
|
+
# "time.lteq" => nil,
|
329
|
+
# }
|
330
|
+
|
331
|
+
# query: { "time.gteq" => "2018-10-01", "time.lteq" => "2018-10-01" }
|
332
|
+
# => query: {
|
333
|
+
# "time.gteq" => Date.parse("2018-10-01 00:00:00"),
|
334
|
+
# "time.lteq" => Date.parse("2018-10-01 23:59:59"),
|
335
|
+
# }
|
336
|
+
```
|
337
|
+
|
338
|
+
- convert by block
|
339
|
+
|
340
|
+
```ruby
|
341
|
+
search.convert do |c|
|
342
|
+
c.convert("name.cont"){|search| search.upcase }
|
343
|
+
end
|
344
|
+
|
345
|
+
# query: { "name.cont" => "search" }
|
346
|
+
# => query: {
|
347
|
+
# "name.cont" => "SEARCH",
|
348
|
+
# }
|
349
|
+
```
|
350
|
+
|
260
351
|
#### search condition
|
261
352
|
|
262
353
|
- not empty
|
@@ -276,6 +367,23 @@ end
|
|
276
367
|
# }
|
277
368
|
```
|
278
369
|
|
370
|
+
- not nil
|
371
|
+
|
372
|
+
```ruby
|
373
|
+
search.query do |q|
|
374
|
+
q.search "name.cont", &q.not_nil
|
375
|
+
end
|
376
|
+
|
377
|
+
# query: { "name.cont" => nil }
|
378
|
+
# => query: {
|
379
|
+
# }
|
380
|
+
|
381
|
+
# query: { "name.cont" => "search" }
|
382
|
+
# => query: {
|
383
|
+
# "name.cont": "search",
|
384
|
+
# }
|
385
|
+
```
|
386
|
+
|
279
387
|
- not all empty
|
280
388
|
|
281
389
|
```ruby
|
data/lib/getto/params/search.rb
CHANGED
@@ -8,24 +8,28 @@ module Getto
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def to_h
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
worker = Worker.new
|
12
|
+
yield worker
|
13
|
+
|
14
|
+
@page.to_h
|
15
|
+
.merge(sort: @sort.to_h(sort: worker.instance_variable_get(:@sort)))
|
16
|
+
.merge(query: @query.to_h(
|
17
|
+
convert: worker.instance_variable_get(:@convert),
|
18
|
+
check: worker.instance_variable_get(:@check),
|
19
|
+
))
|
14
20
|
end
|
15
21
|
|
16
22
|
class Worker
|
17
|
-
def
|
18
|
-
@sort =
|
19
|
-
@query = query
|
20
|
-
@result = result
|
23
|
+
def sort(&block)
|
24
|
+
@sort = block
|
21
25
|
end
|
22
26
|
|
23
|
-
def
|
24
|
-
@
|
27
|
+
def convert(&block)
|
28
|
+
@convert = block
|
25
29
|
end
|
26
30
|
|
27
31
|
def query(&block)
|
28
|
-
@
|
32
|
+
@check = block
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
@@ -48,9 +52,11 @@ module Getto
|
|
48
52
|
@sort = [sort.split(".")].to_h
|
49
53
|
end
|
50
54
|
|
51
|
-
def to_h
|
55
|
+
def to_h(sort:)
|
52
56
|
spec = {}
|
53
|
-
|
57
|
+
if sort
|
58
|
+
sort.call Order.new(spec)
|
59
|
+
end
|
54
60
|
|
55
61
|
result = {
|
56
62
|
column: nil,
|
@@ -92,17 +98,73 @@ module Getto
|
|
92
98
|
@query = query
|
93
99
|
end
|
94
100
|
|
95
|
-
def to_h
|
96
|
-
|
97
|
-
|
101
|
+
def to_h(convert:, check:)
|
102
|
+
converters = {}
|
103
|
+
if convert
|
104
|
+
convert.call Converter.new(converters)
|
105
|
+
end
|
98
106
|
|
99
|
-
|
100
|
-
|
107
|
+
checkers = {}
|
108
|
+
if check
|
109
|
+
check.call Checker.new(checkers)
|
110
|
+
end
|
111
|
+
|
112
|
+
query = @query.map{|key,search|
|
113
|
+
if converter = converters[key.to_s]
|
114
|
+
[key.to_s, converter.call(search)]
|
115
|
+
else
|
116
|
+
[key.to_s, search]
|
117
|
+
end
|
118
|
+
}.to_h
|
119
|
+
|
120
|
+
checkers.map{|key,checker|
|
121
|
+
if search = query[key]
|
101
122
|
if checker.call(search)
|
102
|
-
[key
|
123
|
+
[key, search]
|
103
124
|
end
|
104
125
|
end
|
105
|
-
}.compact.to_h
|
126
|
+
}.compact.to_h.transform_keys(&:to_sym)
|
127
|
+
end
|
128
|
+
|
129
|
+
class Converter
|
130
|
+
def initialize(columns)
|
131
|
+
@columns = columns
|
132
|
+
end
|
133
|
+
|
134
|
+
def convert(column,&converter)
|
135
|
+
@columns[column.to_s] = converter
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
def to_date
|
140
|
+
->(search) {
|
141
|
+
begin
|
142
|
+
::Date.parse(search)
|
143
|
+
rescue ArgumentError
|
144
|
+
nil
|
145
|
+
end
|
146
|
+
}
|
147
|
+
end
|
148
|
+
|
149
|
+
def to_beginning_of_day(time)
|
150
|
+
->(search){
|
151
|
+
begin
|
152
|
+
time.parse(search).to_date.to_time
|
153
|
+
rescue ArgumentError
|
154
|
+
nil
|
155
|
+
end
|
156
|
+
}
|
157
|
+
end
|
158
|
+
|
159
|
+
def to_end_of_day(time)
|
160
|
+
->(search){
|
161
|
+
begin
|
162
|
+
(time.parse(search).to_date + 1).to_time - 1
|
163
|
+
rescue ArgumentError
|
164
|
+
nil
|
165
|
+
end
|
166
|
+
}
|
167
|
+
end
|
106
168
|
end
|
107
169
|
|
108
170
|
class Checker
|
@@ -119,6 +181,10 @@ module Getto
|
|
119
181
|
->(search){ not search.empty? }
|
120
182
|
end
|
121
183
|
|
184
|
+
def not_nil
|
185
|
+
->(search){ not search.nil? }
|
186
|
+
end
|
187
|
+
|
122
188
|
def not_all_empty
|
123
189
|
->(search){ not search.all?(&:empty?) }
|
124
190
|
end
|
data/lib/getto/params/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: getto-params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- shun@getto.systems
|
@@ -94,6 +94,7 @@ files:
|
|
94
94
|
- CHANGELOG/1.1.1.md
|
95
95
|
- CHANGELOG/1.1.2.md
|
96
96
|
- CHANGELOG/1.2.0.md
|
97
|
+
- CHANGELOG/1.3.0.md
|
97
98
|
- Gemfile
|
98
99
|
- Gemfile.lock
|
99
100
|
- LICENSE
|