util-params 0.1.3 → 0.2.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/lib/util/params/params.rb +207 -139
- data/lib/util/params/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fd1ab494a27202fc9796355ecaebcf9b9c67943
|
4
|
+
data.tar.gz: 496d08fbd05d12cd70a112f4c73bf2f214f55b12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a6749be2796880c8e1698d23171f560cb9e3cfdfc8f7d84202e627a84a99d1d0c911c3f69f469a69b224709838aaec1b492ba80dd5f852eb607c94efa304e1d
|
7
|
+
data.tar.gz: 0f1a24934ac17c7854d8165fc5be1107309a28afee87a412495f4512425f107bc58ea8c56219adf0fbea23de3e3897f9138de13cfb37fa8c1f50829baf4fe931
|
data/lib/util/params/params.rb
CHANGED
@@ -8,125 +8,185 @@ module Util
|
|
8
8
|
# エラーリスト
|
9
9
|
@errors = []
|
10
10
|
end
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
11
|
+
|
12
|
+
module Type
|
13
|
+
INTEGER = 1
|
14
|
+
STRING = 2
|
15
|
+
FLOAT = 3
|
16
|
+
FILE = 4
|
17
|
+
BOOLEAN = 5
|
18
|
+
OBJECT = 6
|
19
|
+
ARRAY = 7
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_params options
|
23
|
+
options = _symbolize_keys options
|
24
|
+
|
25
|
+
key = options[:key]
|
26
|
+
val = _load_val params, key, options[:default], options[:require]
|
27
|
+
|
21
28
|
return nil if val.nil?
|
22
|
-
|
29
|
+
|
30
|
+
_validate key, options[:type], val, options
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_int_params key, options
|
34
|
+
get_params options.merge(key: key, type: Type::INTEGER)
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_str_params key, options
|
38
|
+
get_params options.merge(key: key, type: Type::STRING)
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_float_params key, options
|
42
|
+
get_params options.merge(key: key, type: Type::FLOAT)
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_file_params key, options
|
46
|
+
get_params options.merge(key: key, type: Type::FILE)
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_bool_params key, options
|
50
|
+
get_params options.merge(key: key, type: Type::BOOLEAN)
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_array_params key, options
|
54
|
+
get_params options.merge(key: key, type: Type::ARRAY)
|
55
|
+
end
|
56
|
+
|
57
|
+
def get_object_params key, options
|
58
|
+
get_params options.merge(key: key, type: Type::OBJECT)
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
# エラーがあるか
|
63
|
+
def has_params_error?
|
64
|
+
@is_error
|
65
|
+
end
|
66
|
+
# エラーメッセージ入りスト
|
67
|
+
def get_params_error
|
68
|
+
@errors.join ', '
|
69
|
+
end
|
23
70
|
|
24
|
-
|
25
|
-
|
26
|
-
|
71
|
+
protected
|
72
|
+
|
73
|
+
def _load_val vals, key, default, is_require
|
74
|
+
|
75
|
+
unless vals.has_key? key
|
76
|
+
push_error "#{key.to_s} == nil" if is_require
|
77
|
+
return default
|
78
|
+
end
|
79
|
+
|
80
|
+
vals[key]
|
81
|
+
end
|
82
|
+
|
83
|
+
def _validate key_label, type, val, options
|
84
|
+
options ||= {}
|
85
|
+
|
86
|
+
case type
|
87
|
+
when Type::INTEGER
|
88
|
+
_validate_int key_label, val, options[:min], options[:max], options[:enum]
|
89
|
+
when Type::STRING
|
90
|
+
_validate_str key_label, val, options[:min], options[:max], options[:enum], options[:reg]
|
91
|
+
when Type::FLOAT
|
92
|
+
_validate_float key_label, val, options[:min], options[:max]
|
93
|
+
when Type::BOOLEAN
|
94
|
+
_validate_bool key_label, val
|
95
|
+
when Type::FILE
|
96
|
+
_validate_file key_label, val
|
97
|
+
when Type::OBJECT
|
98
|
+
_validate_object key_label, val, options[:elements]
|
99
|
+
when Type::ARRAY
|
100
|
+
vals = _validate_array key_label, val, options[:min], options[:max]
|
101
|
+
return nil if vals.nil?
|
102
|
+
elem_options = options[:element] || {}
|
103
|
+
elem_type = elem_options[:type] || Type::STRING
|
104
|
+
|
105
|
+
vals.map.with_index do |_, i|
|
106
|
+
elem_val = vals[i]
|
107
|
+
_validate "#{key_label}[#{i}]", elem_type, elem_val, elem_options
|
27
108
|
end
|
28
|
-
|
109
|
+
else
|
110
|
+
# do nothing
|
29
111
|
end
|
30
|
-
|
31
|
-
|
32
|
-
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
def _validate_int key, val, min, max, enum
|
116
|
+
return nil if val.blank?
|
117
|
+
|
118
|
+
if /[^\d]/ =~ val.to_s
|
119
|
+
push_error "#{key.to_s} type [#{val.to_s}] != integer"
|
33
120
|
end
|
34
|
-
|
35
|
-
|
36
|
-
|
121
|
+
|
122
|
+
v = val.to_i
|
123
|
+
|
124
|
+
if enum
|
125
|
+
for e in enum
|
126
|
+
return v if e === v
|
127
|
+
end
|
128
|
+
push_error "#{key.to_s} == unknone val [#{v.to_s}]"
|
37
129
|
end
|
38
|
-
|
39
|
-
if
|
40
|
-
push_error "#{
|
130
|
+
|
131
|
+
if min && (v < min)
|
132
|
+
push_error "#{key.to_s} val [#{v.to_s}] < #{min.to_s}"
|
41
133
|
end
|
42
|
-
|
134
|
+
|
135
|
+
if max && (v > max)
|
136
|
+
push_error "#{key.to_s} val [#{v.to_s}] > #{max.to_s}"
|
137
|
+
end
|
138
|
+
|
43
139
|
v
|
44
140
|
end
|
45
|
-
#
|
46
|
-
def get_param_strs name, default = [], is_require = false
|
47
|
-
vals = get_param_val name, default, is_require
|
48
|
-
|
49
|
-
return nil if vals.nil?
|
50
|
-
|
51
|
-
vals
|
52
|
-
end
|
53
141
|
|
54
|
-
|
55
|
-
def get_param_int name, default = nil, is_require = false, params = {}
|
56
|
-
|
57
|
-
val = get_param_val name, default, is_require
|
58
|
-
|
142
|
+
def _validate_str key, val, min, max, enum, reg
|
59
143
|
return nil if val.nil?
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
v = val.to_i
|
67
|
-
|
68
|
-
if params.key? :enum
|
69
|
-
for e in params[:enum]
|
70
|
-
logger.debug e
|
144
|
+
|
145
|
+
v = val.to_s
|
146
|
+
|
147
|
+
if enum
|
148
|
+
enum.each do |e|
|
71
149
|
return v if e === v
|
72
150
|
end
|
73
|
-
push_error "#{
|
151
|
+
push_error "#{key.to_s} == unknone val [#{v.to_s}]"
|
74
152
|
end
|
75
|
-
|
76
|
-
if
|
77
|
-
push_error "#{
|
153
|
+
|
154
|
+
if min && (v.length < min)
|
155
|
+
push_error "#{key.to_s}.length < #{min.to_s} ('#{v.to_s}')"
|
78
156
|
end
|
79
|
-
|
80
|
-
if
|
81
|
-
push_error "#{
|
157
|
+
|
158
|
+
if max && (v.length > max)
|
159
|
+
push_error "#{key.to_s}.length > #{max.to_s} ('#{v.to_s}')"
|
82
160
|
end
|
161
|
+
|
162
|
+
if reg && !(/#{reg}/ =~ val)
|
163
|
+
push_error "#{key.to_s} unmatch /#{reg.to_s}/ =~ [#{v.to_s}]"
|
164
|
+
end
|
165
|
+
|
83
166
|
v
|
84
167
|
end
|
85
168
|
|
86
|
-
def
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
vals.each do |v|
|
93
|
-
|
94
|
-
if params.key? :enum
|
95
|
-
for e in params[:enum]
|
96
|
-
next rs << v.to_i if e === v
|
97
|
-
end
|
98
|
-
push_error "#{name.to_s} == unknone val [#{v.to_s}]"
|
99
|
-
end
|
100
|
-
|
101
|
-
if params.key?(:min) && (v.length < params[:min])
|
102
|
-
push_error "#{name.to_s} val [#{v.to_s}] < #{params[:min].to_s}"
|
103
|
-
end
|
104
|
-
|
105
|
-
if params.key?(:max) && (v.length > params[:max])
|
106
|
-
push_error "#{name.to_s} val [#{v.to_s}] > #{params[:max].to_s}"
|
107
|
-
end
|
108
|
-
|
109
|
-
rs << v.to_i
|
169
|
+
def _validate_float key, val, min, max
|
170
|
+
return nil if val.blank?
|
171
|
+
|
172
|
+
if /[^\d.]/ =~ val
|
173
|
+
push_error "#{key.to_s} type [#{val.to_s}] != float"
|
110
174
|
end
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
vals = get_param_val name, default, is_require
|
117
|
-
vals.map do |v|
|
118
|
-
v.permit!.to_h.deep_symbolize_keys
|
175
|
+
|
176
|
+
v = val.to_f
|
177
|
+
|
178
|
+
if min && (v < min)
|
179
|
+
push_error "#{key.to_s} val [#{v.to_s}] < #{min.to_s}"
|
119
180
|
end
|
181
|
+
|
182
|
+
if max && (v > max)
|
183
|
+
push_error "#{key.to_s} val [#{v.to_s}] > #{max.to_s}"
|
184
|
+
end
|
185
|
+
|
186
|
+
v
|
120
187
|
end
|
121
|
-
|
122
|
-
def
|
123
|
-
val = get_param_val name, default, is_require
|
124
|
-
val.permit!.to_h.deep_symbolize_keys
|
125
|
-
end
|
126
|
-
|
127
|
-
#
|
128
|
-
def get_param_bool name, default = nil, is_require = false
|
129
|
-
val = get_param_val name, default, is_require
|
188
|
+
|
189
|
+
def _validate_bool key, val
|
130
190
|
return false if val.kind_of? FalseClass
|
131
191
|
return true if val.kind_of? TrueClass
|
132
192
|
return nil if val.blank?
|
@@ -134,59 +194,67 @@ module Util
|
|
134
194
|
return true if val == 'true'
|
135
195
|
val.to_i > 0
|
136
196
|
end
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
f
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
if f.nil?
|
146
|
-
push_error "#{name.to_s} == nil"
|
147
|
-
return nil
|
148
|
-
end
|
149
|
-
if f.size.blank?
|
150
|
-
push_error "#{name.to_s} == nil"
|
151
|
-
return nil
|
152
|
-
end
|
197
|
+
|
198
|
+
def _validate_file key, val
|
199
|
+
return nil if val.nil?
|
200
|
+
|
201
|
+
if f.size.blank?
|
202
|
+
push_error "#{key.to_s} == nil"
|
203
|
+
return nil
|
153
204
|
end
|
154
|
-
|
155
|
-
return nil if
|
156
|
-
return nil if
|
157
|
-
|
158
|
-
|
205
|
+
|
206
|
+
return nil if val.nil?
|
207
|
+
return nil if val.size.blank?
|
208
|
+
|
209
|
+
val
|
159
210
|
end
|
160
|
-
|
161
|
-
def
|
162
|
-
|
211
|
+
|
212
|
+
def _validate_array key, val, min, max
|
213
|
+
return nil if val.nil?
|
214
|
+
|
215
|
+
unless val.kind_of? Array
|
216
|
+
push_error "#{key.to_s}.type != Array"
|
217
|
+
return nil
|
218
|
+
end
|
219
|
+
|
220
|
+
v = val
|
221
|
+
|
222
|
+
if min && (v.length < min)
|
223
|
+
push_error "#{key.to_s} val [#{v.to_s}.length] < #{min.to_s}"
|
224
|
+
end
|
225
|
+
|
226
|
+
if max && (v.length > max)
|
227
|
+
push_error "#{key.to_s} val [#{v.to_s}.length] > #{max.to_s}"
|
228
|
+
end
|
229
|
+
|
230
|
+
v
|
163
231
|
end
|
164
|
-
|
165
|
-
def
|
166
|
-
|
232
|
+
|
233
|
+
def _validate_object key, val, elements
|
234
|
+
return nil if val.nil?
|
235
|
+
|
236
|
+
elements.map do |options|
|
237
|
+
options ||= {}
|
238
|
+
elem_key = options[:key]
|
239
|
+
elem_type = options[:type]
|
240
|
+
elem_default = options[:default]
|
241
|
+
elem_require = options[:require]
|
242
|
+
elem_val = _load_val val, elem_key, elem_default, elem_require
|
243
|
+
|
244
|
+
_validate "#{key}[#{elem_key}]", elem_type, elem_val, options
|
245
|
+
end
|
167
246
|
end
|
168
|
-
|
169
|
-
def
|
170
|
-
|
247
|
+
|
248
|
+
def _symbolize_keys hash
|
249
|
+
hash.map{|k,v| [k.to_sym, v] }.to_h
|
171
250
|
end
|
172
|
-
|
173
|
-
protected
|
174
|
-
|
251
|
+
|
175
252
|
# エラー追加
|
176
253
|
def push_error message
|
177
254
|
@is_error |= true
|
178
255
|
@errors.push message
|
179
256
|
end
|
180
257
|
|
181
|
-
# パラメーター取得
|
182
|
-
def get_param_val name, default, is_require
|
183
|
-
unless params.include? name
|
184
|
-
push_error "#{name.to_s} == nil" if is_require
|
185
|
-
return default
|
186
|
-
end
|
187
|
-
params[name]
|
188
|
-
end
|
189
|
-
|
190
258
|
end
|
191
259
|
|
192
260
|
end
|
data/lib/util/params/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: util-params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- unchi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|